Friday, November 19, 2010

/* Program for binary search using recursion*/

/* Program for binary search in c*/

#include<stdio.h>
#include<conio.h>

int binsearch(int [],int,int,int);
void main()
            {
            int ar[]={12,23,32,34,45};
            int i,lb=0,ub=4,ser;
            clrscr();
            printf("Enter search ele :-");
            scanf("%d",&ser);
            int f=binsearch(ar,ser,lb,ub);
            if(f == 1)
                        printf("Ele found....");
            else
                        printf("Ele not found...");
            getch();
            }

int binsearch(int ar[],int ser,int lb,int ub)
            {
            int mb=(lb+ub)/2;
            if(lb>ub)
                        return 0;
            if(ar[mb] == ser)
                        return 1;
            if(ser>ar[mb])
                        {
                        lb=mb+1;
                        binsearch(ar,ser,lb,ub);
                        }
            if(ser<ar[mb])
                        {
                        ub=ub-1;
                        binsearch(ar,ser,lb,ub);
                        }
            }



/* Program for binary search in java*/

class binsearchprog
{
public void main()
            {
            int ar[]={12,23,32,34,45};
            int i,lb=0,ub=4,ser;
            ser=12;
            int f=binsearch(ar,ser,lb,ub);
            if(f == 1)
                        System.out.println("Ele found....");
            else
                        System.out.println("Ele not found...");
            }

int binsearch(int ar[],int ser,int lb,int ub)
            {
            int mb=(lb+ub)/2;
            if(lb>ub)
                        return 0;
            if(ar[mb] == ser)
                        return 1;
            if(ser>ar[mb])
                        {
                        lb=mb+1;
                        binsearch(ar,ser,lb,ub);
                        }
            if(ser<ar[mb])
                        {
                        ub=ub-1;
                        binsearch(ar,ser,lb,ub);
                        }
            }

/* Program for selection sort recursion process*/

/* program for c */ 

#include<stdio.h>
#include<string.h>
#include<conio.h>
void selectionsort(int [],int);
void main()
            {
            int ar[]={12,54,23,45,6};
            int i;
            printf("Before sort\n");
            for(i=0;i<5;i++)
                        printf("%d\t",ar[i]);
            selectionsort(ar,0);
            printf("\nAfter sort\n");
            for(i=0;i<5;i++)
                        printf("%d\t",ar[i]);

            }

void selectionsort(int array[], int startIndex)
{
    if ( startIndex >= 4 )
            return;
    int minIndex = startIndex;
    for ( int index = startIndex + 1; index < 5; index++ )
    {
            if (array[index] < array[minIndex] )
                minIndex = index;
    }
    int temp = array[startIndex];
    array[startIndex] = array[minIndex];
    array[minIndex] = temp;
    selectionsort(array, startIndex + 1);
}

/* program for java */ 
class selectionsortrecursion
{
public void main()
            {
            int ar[]={12,54,23,45,6};
            int i;
            System.out.print("Before sort\n");
            for(i=0;i<5;i++)
                        System.out.print(ar[i]+"\t");
            selectionsort(ar,0);
            System.out.print("\nAfter sort\n");
            for(i=0;i<5;i++)
                        System.out.print(ar[i]+"\t");

            }

void selectionsort(int array[], int startIndex)
{
    if ( startIndex >= 4 )
            return;
    int minIndex = startIndex;
    for ( int index = startIndex + 1; index < 5; index++ )
    {
            if (array[index] < array[minIndex] )
                minIndex = index;
    }
    int temp = array[startIndex];
    array[startIndex] = array[minIndex];
    array[minIndex] = temp;
    selectionsort(array, startIndex + 1);
}

Thursday, November 18, 2010

program for implement queue using array in java

import java.io.*;
class queue
    {
        public static void main()throws IOException
            {
                BufferedReader br=new BufferedReader(
                    new InputStreamReader(System.in));
                int ar[]=new int[5];
                int rear,front,ch;
                rear=front=-1;
                do
                    {
                        System.out.println("1.Insert element");
                        System.out.println("2.Delete element");
                        System.out.println("3.Display element");
                        System.out.println("4.Exit");
                        System.out.print("Enter your choice :-");
                        ch=Integer.parseInt(br.readLine());
                        switch(ch)
                            {
                                case 1:
                                    if((rear+1)>4)
                                        System.out.println("Queue overflow...");
                                    else
                                        {
                                            System.out.print("Enter element :-");
                                            ar[++rear]=Integer.parseInt(br.readLine());            
                                        }
                                    if(front == -1)
                                        front=0;
                                    break;
                                case 2:
                                    if(front == -1)
                                        System.out.println("Queue underflow....");
                                    else
                                        System.out.println("Poped element :-"+ar[front++]);
                                      
                                    if(front>rear)
                                         front=rear=-1;
                                    break;
                                case 3:
                                    if(front == -1)
                                        System.out.println("Queue underflow....");
                                    else
                                        {
                                            for(int i=front;i<=rear;i++)
                                                {
                                                    System.out.print(ar[i]+"\t");
                                                }
                                            System.out.println();
                                         
                                        }
                                    break;
                                case 4:
                                    System.out.println("Program end....");
                                    break;
                                default:
                                    System.out.println("Invalid choice enter....");
                            }
                    }while(ch != 4);
            }
    }

prog for smith number check in java

import java.io.*;
class pro_smith
{
public void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter numbers");
int n=Integer.parseInt(br.readLine());
int copy=0,rem=0,i,j,sumd=0,sumf=0;
      
       copy=n;
       sumd=sum_digit(copy);
      
       for(i=2;i<=7;i++)
       {
           if(n%i==0)
           {
               if((i!=4)&&(i!=6))
               {
                   sumf=sumf+i;
                   n=n/i;
                   i--;
                }
            }    
        }
               for(i=11;i<=n;i++)
               {
                   if(n%i==0)
                   {
                       copy=i;
                       for(j=11;j<=i;j++)
                       {
                           if(i%j==0)
                           break;
                        }
                        if(j==i)
                        {
                            sumf=sumf+(sum_digit(copy));
                            n=n/i;
                            i--;
                        }
                    }
                }
                if(sumd==sumf)
                System.out.println("smith number");
                else
                 System.out.println("not a smith number");
                }
                public int sum_digit(int a)
                {
                    int rem=0,sum=0;
                   
                        while(a>0)
                        {
                            rem=a%10;
                            sum=sum+rem;
                            a=a/10;
                        }
                        return sum;
                    }
                }
                   

program for display nearest prime number

class nprime
{
public void nprime(int n)
{
int up,dn,i;
up=n+1;

      while(true)
      {
          i=isprime(up);
          if(i==0)
          break;
          up++;
       }
              dn=n-1;
              while(true)
              {
                  i=isprime(dn);
                  if(i==0)
                  break;
                  dn--;
               }
                System.out.print("the nearest prime number is");
                  if((n-dn)<(up-n))
                  System.out.println(dn);
                  else if((up-n)<(n-dn))
                 System.out.println(up);
                 else
                 System.out.println(up+" "+dn);
 }               
     
              public int isprime(int a)
              {   int f=0;
                  for(int j=2;j<=a/2;j++)
                  {
                      if (a%j==0)
                      {
                          f=1;break;
                        }}
                        return f;
                    }
                }

program for convert number to word in java

class no_to_word
{
public static  void notoword(int n)
{
String once[]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty",};
String tens[]={"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
if(!(n<=1)&&(n>=100))
System.out.println("invalid");
else
{
if(n<20)
System.out.println(once[n]);
else
{
int i=n/10;
n=n%10;
if(n==0)
System.out.print(tens[i-2]);
else
System.out.println(tens[i-2]+" "+once[n]);
    }
}
    }
}

program for merge sort in java

class merge
{
public static void main( )
{
 int a[] = { 11, 2, 9, 13, 57 } ;
 int b[] = { 25, 17, 1, 90, 3 } ;
 int c[]=new int[10] ;
 int i, j, k, temp ;

 System.out.println( "Merge sort.\n" ) ;
 System.out.println( "\nFirst array:\n" ) ;
 for ( i = 0 ; i <= 4 ; i++ )
  System.out.println( a[i]+" " ) ;
 System.out.println ( "\n\nSecond array:\n" ) ;
 for ( i = 0 ; i <= 4 ; i++ )
  System.out.println( b[i] +" ") ;
 for ( i = 0 ; i <= 3 ; i++ )
 {
  for ( j = i + 1 ; j <= 4 ; j++ )
  {
   if ( a[i] > a[j] )
   {
    temp = a[i] ;
    a[i] = a[j] ;
    a[j] = temp ;
   }
   if ( b[i] > b[j] )
   {
    temp = b[i] ;
    b[i] = b[j] ;
    b[j] = temp ;
   }
  }
 }
 for ( i = j = k = 0 ; i <= 9 ; )
 {
  if ( a[j] <=  b[k] )
   c[i++] = a[j++] ;
  else
   c[i++] = b[k++] ;
  if ( j == 5 || k == 5 )
   break ;
 }
 for ( ; j <= 4 ; )
  c[i++] = a[j++] ;
 for ( ; k <= 4 ; )
  c[i++] = b[k++] ;
 System.out.println ( "\n\nArray after sorting:\n") ;
 for ( i = 0 ; i <= 9 ; i++ )
  System.out.println( c[i] +" ") ;
     }
}

program to display lucky number in java

import java.io.*;
class lucky
    {
        public static void meth()throws IOException
        {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   
    System.out.println("enter limit");
    int n=Integer.parseInt(br.readLine());
   int ar[]=new int[n];
   for(int i=0;i<n;i++)
    ar[i]=i+1;
      
       int a,b,c,d;
        a=1;
      
        b=ar.length;
        int gap=2;
        while(true)
        {
        for(c=a;c<b;c=c+gap)
            {
                ar[c]=-1;
            }
        d=0;
        for(c=0;c<b;c++)
            {
                if(ar[c] != -1)
                    d++;
            }
        int temp[]=new int[d];
        int p=0;
        for(c=0;c<b;c++)
            {
                if(ar[c] != -1)
                    temp[p++]=ar[c];
                ar[c]=-1;
            }
         for(c=0;c<p;c++)
            {
                ar[c]=temp[c];
            }
         a=a+1;
         gap++;
         if(a>d)
            {
                break;
            }
        }
      int res=0;
      for(int i=0;i<8;i++)
        {
            if(ar[i] != -1)
                {
                    int digi=ar[i];
                    c=0;
                    while(digi>0)
                        {
                            c++;
                            digi/=10;
                        }
                    double t=Math.pow(10,c);
                    res=(res*(int)t)+ar[i];
                }
            }
         System.out.println(res);
       }
    }

program to display lucky number in java

import java.io.*;
class lucky
    {
        public static void meth()throws IOException
        {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   
    System.out.println("enter limit");
    int n=Integer.parseInt(br.readLine());
   int ar[]=new int[n];
   for(int i=0;i<n;i++)
    ar[i]=i+1;
      
       int a,b,c,d;
        a=1;
      
        b=ar.length;
        int gap=2;
        while(true)
        {
        for(c=a;c<b;c=c+gap)
            {
                ar[c]=-1;
            }
        d=0;
        for(c=0;c<b;c++)
            {
                if(ar[c] != -1)
                    d++;
            }
        int temp[]=new int[d];
        int p=0;
        for(c=0;c<b;c++)
            {
                if(ar[c] != -1)
                    temp[p++]=ar[c];
                ar[c]=-1;
            }
         for(c=0;c<p;c++)
            {
                ar[c]=temp[c];
            }
         a=a+1;
         gap++;
         if(a>d)
            {
                break;
            }
        }
      int res=0;
      for(int i=0;i<8;i++)
        {
            if(ar[i] != -1)
                {
                    int digi=ar[i];
                    c=0;
                    while(digi>0)
                        {
                            c++;
                            digi/=10;
                        }
                    double t=Math.pow(10,c);
                    res=(res*(int)t)+ar[i];
                }
            }
         System.out.println(res);
       }
    }

program for display next vowel if vowel and display next consonent if consonent in java

import java.util.*;
class letter
    {
        public void wordsort(String st)
            {
                StringTokenizer stk=new StringTokenizer(st);
                int i=stk.countTokens();
               
                for(int j=1;j<=i;j++)
                    {
                        String wrd=stk.nextToken();
                    }
                   String res=sort(st);
                       String rest=encode(res)+" ";
                        System.out.println(rest);
                   
            }
         public String sort(String wrd)
            {  
               int g=0,i,j,d=0;
               String p="",res="";wrd+=" ";
               int l=wrd.length();
               for (j=0;j<l;j++)
                {
                    if(wrd.charAt(j)==' ')
                    d++;
                }
                String arr[]=new String[d];
                for(i=0;i<=l-1;i++)
                    { 
                       
                        if(wrd.charAt(i)!=' ')
                        p=p+wrd.charAt(i);
                       
                        else 
                      {
                          arr[g]=p;
                      p="";
                        g++;
                    }
                }                           
            
               
                for(i=0;i<arr.length;i++)
                    {  int  pos=i;
                        for(j=(i+1);j<arr.length;j++)
                            {
                               if ( arr[j].compareTo(arr[pos]) <0)
                                    pos=j;
                                }      
                                       String temp =arr[pos];
                                       arr[pos] = arr[i] ;
                                        arr[i]=temp;
                                    }
                                   
                  
                   
                 for(i=0;i<arr.length;i++)
                    res+=arr[i]+" ";  
                return(res);
            }
         public String encode(String st)
            {
                String cvow="AEIOU",svow="aeiou",ccon="BCDFGHJKLMNPQRSTVWXY",scon="bcdfghjklmnpqrstvwxy";
                int i,j,k,l;
                char ch;
                String res="";
                for(i=0;i<st.length();i++)
                    {   
                        ch=st.charAt(i);
                        if(ch == 'U')
                            res+='A';
                        else if(ch == 'u')
                            res+='a';
                        else if(ch == 'Z')
                            res+='B';
                        else if(ch == 'z')
                            res+='b';
                        else if(ch==' ')
                        res+=' ';
                        else
                            {
                                k=0;
                                for(j=0;j<cvow.length();j++)
                                    {
                                        if(ch == cvow.charAt(j))
                                            {
                                                res+=cvow.charAt(j+1);
                                                k=1;
                                                break;
                                            }
                                    }
                                if(k == 0)
                                {
                                for(j=0;j<svow.length();j++)
                                    {
                                        if(ch == svow.charAt(j))
                                            {
                                                res+=svow.charAt(j+1);
                                                k=1;
                                                break;
                                            }
                                    }
                                }
                                if(k == 0)
                                {
                                for(j=0;j<scon.length();j++)
                                    {
                                        if(ch == scon.charAt(j))
                                            {
                                                res+=scon.charAt(j+1);
                                                break;
                                            }
                                    }
                                }
                                if(k == 0)
                                {
                                for(j=0;j<ccon.length();j++)
                                    {
                                        if(ch == ccon.charAt(j))
                                            {
                                                res+=ccon.charAt(j+1);
                                                break;
                                            }
                                    }
                                }
                             
                            }                           
                           
                    }
                  return(res);
            }
    }

program for display lcm of n numbers in java[ different way]

import java.io.*;
class lcm_f_n_nos
{
    public static void meth()throws IOException
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,no1,no2,lcm=1,i;
System.out.println("enter no of numbers");
 n=Integer.parseInt(br.readLine());
while(n>0)
{
no1=lcm;
lcm=1;
System.out.println("enter nunbers");
no2=Integer.parseInt(br.readLine());
for(i=2;i<=(no1*no2);i++)
{
if((no1%i==0)&&(no2%i==0))
{
lcm=lcm*i;
no1=no1/i;
no2=no2/i;
i=i-1;
    }
}
lcm=lcm*no1*no2;
    n=n-1;
}
System.out.println("LCM:"+lcm);
    }
}

program for display lcm of n numbers in java

import  java.io.*;
class lcm
    {
        public static void main() throws IOException
            {
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                int lcm=1,a,b,c,flag;
                System.out.print("Enter total number of time : - ");
                a=Integer.parseInt(br.readLine());
                System.out.print("Enter number : - ");
                b=Integer.parseInt(br.readLine());
                for(int i=1;i<a;i++)
                    {
                        lcm=1;
                        System.out.print("Enter number : - ");
                        c=Integer.parseInt(br.readLine());
                        while(true)
                        {
                        int max=(b>c?b:c);
                        max/=2;
                        int d=2;
                        flag=0;
                        while(d<=max)
                            {
                               if((b%d == 0) && (c%d ==0))
                                {
                                    b/=d;
                                    c/=d;
                                    lcm*=d;
                                    flag=1;
                                    break;
                                }
                                d++;
                            }
                         if(flag == 0)
                            break;
                        }
                    lcm*=b;
                    lcm*=c;
                    b=lcm;
                      }
              System.out.println(lcm);
            }
    }

program for check if a number is a keith number or not in java

import java.io.*;
class keith_number
{
public static void meth()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("enter number");
    int n=Integer.parseInt(br.readLine());
     int temp=n,t=n,r,re=-1,a,b,c;  
        while(n>0)
        {
            r=n%10;
            re++;
            n=n/10;
        }
        b=temp%((int)Math.pow(10,re));
        a=temp/((int)Math.pow(10,re));
          System.out.println(a);
          System.out.println(b) ;
             while(true)
             {
                 c=a+b;
                System.out.println(c) ;
        
                 if(c>=t)
                 break;
                   a=b;
                 b=c;
                
                }
                if (c==t)
                 System.out.println("keith number");
                 else
                 System.out.println("not a keith number");
                
                }
            }

program for display if a number is a kaprekar number or not

//eg.297*297=88209     88+209=297
class kaprakar_number
{
public static void meth(int n)
{
     int temp,k,a=0,r,rem,quo;
     temp=n;
     k=n*n;
         
         
            while(n>0)
            {
                r=n%10;
                a++;
                n=n/10;
            }
           
            rem=k%((int)Math.pow(10,a));
            quo=k/((int)Math.pow(10,a));
           
           
           
            if((rem+quo)==temp)
            System.out.println("kaprakar number");
            else
              System.out.println(" not kaprakar number");
            }
        }