Wednesday, December 1, 2010

PRG FOR DISPLAY SEVEN ITERATION NUMBER

class seven_iteration_number
    {
        public int deasc(int n)
            {
                int t,a,i,no=0;
                for(i=9;i>=0;i--)
                    {
                        t=n;
                        while(t>0)
                            {
                                a=t%10;
                                if(a == i)
                                    no=(no*10)+a;
                                t/=10;
                            }
                    }
               return(no);
            }
        public int asc(int n)
            {
                int t,a,i,no=0;
                for(i=1;i<10;i++)
                    {
                        t=n;
                        while(t>0)
                            {
                                a=t%10;
                                if(a == i)
                                    no=(no*10)+a;
                                t/=10;
                            }
                    }
               return(no);
            }
       public int no_of_digit(int t)
            {
                int c=0;
                while(t>0)
                    {
                        c++;
                        t/=10;
                    }
               return(c);
            }
        public void chk_seven_iteration_number(int n)
            {
                int as,ds,dig,dig1,diff;
                do
                    {
                        as=asc(n);
                        ds=deasc(n);
                        diff=ds-as;
                         n=diff;
                        if(diff == 6174)
                            {
                                System.out.println(ds + "\t - \t"+as+"\t = \t " +diff);
                                break;
                            }
                        else
                                System.out.println(ds + "\t - \t"+as+"\t = \t " +diff);
                    }while(true);
            }
    }

pRG FOR IMPLEMENT STACK WITH ARRAY USING JAVA

import java.io.*;
class Stack
{
    int arr[]=new int[10];
    int len=0;
    Stack()
    {
        for(int i=0;i<10;i++)
            arr[i]=0;
    }
    void push(int a)
    {
        if(len<10)
        {
            arr[len]=a;
            len++;
        }
        else System.out.println("Cannot Input more than 10 Numbers");
    }
    int pop()
    {
        if(len>0)
            return arr[len-1];
        else return -1;
    }
    int popAndTop()
    {
        if(len>0)
            return arr[len--];          
        else return -1;
    }
    void main()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int ch;
        Stack pile=new Stack();
        do
        {
            System.out.println("\tList\n1.Push\n2.Pop\n3.Pop And Top\n4.Exit");
            ch=Integer.parseInt(br.readLine());
            switch(ch)
            {
                case 1:System.out.println("Enter Element");
                       int n=Integer.parseInt(br.readLine());
                       pile.push(n);
                       break;
                case 2:if(pile.pop()!=-1)
                        System.out.println("\t"+pile.pop());
                       else System.out.println("No Elements to Display");
                       break;
                case 3:if(pile.popAndTop()!=-1)
                        System.out.println("\t"+pile.popAndTop());
                       else System.out.println("No Elements to Display");                      
                       break;
                case 4:System.out.println("Goodbye");break;
                default:System.out.println("Invalid choice");
            }
        }while(ch!=4);
    }
}

pRG FOR READ A STRING AND CONVERT ALL VOWEL TO ITS NEXT VOWEL AND ALL CONSONENT TO ITS NEXT CONSONENT

class strings
  {
      void chk(String st)
      {
          int i,j,k,m;
          i=st.length();
          char ch;
          String wrd="";
          Character arr[]={'a','e','i','o','u'};
          for(j=0;j<i;j++)
          {
              ch=st.charAt(j);
              if(ch=='z')
               wrd+='b';
              else if(!(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'))
                 {
                  k=(int)ch;
                  k++;
                  if(((char)k=='a'||(char)k=='e'||(char)k=='i'||(char)k=='o'||(char)k=='u'))
                  {
                  k=(int)(char)k;
                  k++;
                  wrd+=(char)(int)(char)k;
                  }
                else
                wrd+=(char)k;
            }
           else
           {
          for(m=0;m<5;m++)
              { if(ch==arr[m] && ch!=arr[0])
                 wrd+=arr[m-1];
               }
                    if(ch==arr[0])
                       wrd+=arr[4];
           }
        }
       
        System.out.println(wrd);
    }
   
}

Prog for read a number and display all sets of sum of concetuive number

class sum_consucative
    {
        public void chk_sum_consucative(int n)
            {
                int i,j,t,f,chk=0,a;
                for(i=1;i<n;i++)
                    {
                        t=0;
                        f=0;
                        for(j=i;j<=n;j++)
                            {
                                t+=j;
                                if( t == n)
                                    {
                                        chk=1;
                                        f=1;
                                        break;
                                    }
                                else if(t>n)
                                    break;
                            }
                       if( f == 1)
                            {
                                for(a=i;a<j;a++)
                                    System.out.print(a+" + ");
                                System.out.print(a + " = " + n);
                                System.out.println();
                            }
                    }
                 if( chk == 0)
                    System.out.print("Sorry set not possiable................");
            }
    }

Prog for display tic tac toe using java

import java.io.*;
class tictactoe
{
  char arr[][]={{'_','_','_'},{'_','_','_'},{'_','_','_'}};
 public void display()
 {
     int r,c;
     for(r=0;r<3;r++)
      {
          for(c=0;c<3;c++)
          System.out.print(arr[r][c]+"\t");
          System.out.println();
        }
    }
    public int iswin()
    {
        int k=0;
        if((arr[0][1]=='o'&&arr[0][2]=='o')||(arr[1][0]=='o'&&arr[2][0]=='o'))
        k=1;
        if((arr[0][0]=='x' && arr[0][1]=='x' && arr[0][2]=='x')||(arr[0][0]&&arr[1][0]=='x' && arr[2][0]=='x'))
        k=2;      
        if((arr[2][2]=='o'&&arr[0][2]=='o'&&arr[1][2]=='o')||(arr[0][0]&&arr[2][0]=='o'&&arr[2][1]=='o'))
        k=1;
        if((arr[2][2]=='x'&&arr[0][2]=='x'&&arr[1][2]=='x')||(arr[0][0]&&arr[2][0]=='x'&&arr[2][1]=='x'))       
        k=2;   
        if((arr[1][1]=='o'&&arr[0][0]=='o'&&arr[2][2]=='o')||(arr[1][1]=='o'&&arr[2][0]=='o'&&arr[0][2]=='o')||
           (arr[1][1]=='o'&&arr[0][1]=='o'&&arr[2][1]=='o')||(arr[1][1]=='o'&&arr[1][0]=='o'&&arr[1][2]=='o'))
        k=1;
        if((arr[1][1]=='x'&&arr[0][0]=='x'&&arr[2][2]=='x')||(arr[1][1]=='x'&&arr[2][0]=='x'&&arr[0][2]=='x')||
           (arr[1][1]=='x'&&arr[0][1]=='x'&&arr[2][1]=='x')||(arr[1][1]=='x'&&arr[1][0]=='x'&&arr[1][2]=='x'))
        k=2;
        return k;
    }
    public void main()throws IOException
    {
       
        int i,r,c;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter player 1 ");
        String n1=br.readLine();
        System.out.println("Enter player 2 ");
        String n2=br.readLine();
        System.out.println(n1+" gets o");
        System.out.println(n2+" gets x");
        for(i=1;i<=5;i++)
        {
            System.out.println(n1+"'s turn");
            System.out.println("Enter row");
            r=Integer.parseInt(br.readLine());
            System.out.println("Enter column");
            c=Integer.parseInt(br.readLine());
            if(arr[r][c]!='_')
            {
            System.out.println("Position occupied");
            break;
            }
            else
            arr[r][c]='o';
            display();
            if(iswin()==1)
            {
                System.out.println(n1+" wins!");
                break;
            }
            else if(iswin()==2)
            {
                System.out.println(n2+" wins!");
                break;
            }
            if(i==5)
            break;
            System.out.println(n2+"'s turn");
            System.out.println("Enter row");
            r=Integer.parseInt(br.readLine());
            System.out.println("Enter column");
            c=Integer.parseInt(br.readLine());
            if(arr[r][c]!='_')
            {
            System.out.println("Position occupied");
            break;
            }
            else
            arr[r][c]='x';
            display();
           
           
           
        }
        if(iswin()==0)
        System.out.println("Draw!!!");
    }
}
           
           
       

Prog for convert time in word format in java

import java.io.*;
class TimeInWords
 {
     public void display_time()throws IOException
     {
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         System.out.println("Enter hours.....(1-12)");
         int hh=Integer.parseInt(br.readLine());
         System.out.println("Enter minutes.....(0-59)");
         int mm=Integer.parseInt(br.readLine());
         String time[]={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine",
             "Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen",
             "Eighteen","Nineteen","Twenty","Twenty One","Twenty Two","Twenty Three",
             "Twenty Four","Twenty Five","Twenty Six","Twenty Seven","Twenty Eight",
             "Twenty Nine"};
             if(hh>12 || mm>60)
             System.out.println("The Time is invalid");
             else if(mm==0)
             System.out.println("The time is "+time[hh-1]+" o'Clock");
             else if(mm<30 && mm!=15)
             System.out.println("The time is "+time[mm-1]+" minute(s) past "+time[hh-1]);
             else if(mm==15)
             System.out.println("The time is quarter past "+time[hh-1]);
             else if(mm==30)
             System.out.println("The time is "+time[hh-1] + " Thirty");
             else if(mm==45 && hh!=12)
             System.out.println("The time is quarter to "+time[hh]);
             else if(mm==45 && hh==12)
             System.out.println("The time is quarter to "+time[0]);
             else if(mm>30 && hh!=12)
             System.out.println("The time is "+time[60-mm-1]+" minute(s) to "+time[hh]);
             else if(hh==12 && mm>30)
             System.out.println("The time is "+time[60-mm-1]+" minute(s) to "+time[0]);
            
            }
        }
       
            
            
            
            
            
            
            
            

Transpose array without any other array in java

import java.io.*;
class transpose_1array
  {
      public void transpose()throws IOException
      {
          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
          int i,j,t=0;
          System.out.println("Enter the size of the array");
          int n=Integer.parseInt(br.readLine());
          int arr[][]=new int[n][n];
          for(i=0;i<n;i++)
          {
              for(j=0;j<n;j++)
              {
                  System.out.println("Enter element at "+i+","+j);
                  arr[i][j]=Integer.parseInt(br.readLine());
                }
            }
           for(i=0;i<n;i++)
           {
               for(j=0;j<i;j++)
               {
                   t=arr[i][j];
                   arr[i][j]=arr[j][i];
                   arr[j][i]=t;
                }
            }
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                System.out.print(arr[i][j] + "  ");
                 System.out.println();
            }
          
        }
    }

To display all union ele from two different single dimension array in java

import java.io.*;
class union
 {
     public void main()throws IOException
     {
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         System.out.print("Enter the size of array1 :- ");
         int s1=Integer.parseInt(br.readLine());
         System.out.print("Enter the size of array2 :- ");
         int s2=Integer.parseInt(br.readLine());
         int a[]=new int[s1];
         int b[]=new int[s2];
         int c[]=new int[s1+s2];
         int i,j,pos1=0,pos2=0,pos3=0,f=0;
         System.out.print("Enter elements of array1 ");
         for(i=0;i<s1;i++)
         a[i]=Integer.parseInt(br.readLine());
         System.out.print("Enter elements of array2 ");
         for(i=0;i<s2;i++)
         b[i]=Integer.parseInt(br.readLine());
         for(i=0;i<s1;i++)
         {f=0;
             for(j=0;j<i;j++)
             {
                 if(a[i]==a[j])
                 {
                     f=1;
                     break;
                    }
                }
                if(f==0)
                {
                    a[pos1++]=a[i];
                    c[pos3++]=a[i];
                }
            }
         
           
          
           for(i=0;i<s2;i++)
             {f=0;
             for(j=0;j<i;j++)
             {
                 if(b[i]==b[j])
                 {
                     f=1;
                     break;
                    }
                }
                if(f==0)
                b[pos2++]=b[i];
                   
            }
          
          for(i=0;i<pos2;i++)
          {f=0;
              for(j=0;j<pos1;j++)
              {
                  if(b[i]==a[j])
                  {
                      f=1;
                      break;
                }}
                if(f==0)
                c[pos3++]=b[i];
            }
            for(i=0;i<pos3;i++)
            System.out.print(c[i]+" ");
        }
    }
           
            

To find n digit unique number in java

import java.io.*;
class UniqueNo
{
long countDigits(long no)
{
long copy = no,count = 0;
while(copy!=0)
{
count++;
copy/=10;
}
return count;
}
boolean checkUnique(long n)
{
int nd = (int)countDigits(n);
long arr[] = new long[nd];
long copy = n;
int digit,term = 0;
while(copy!=0)
{
digit = (int)copy%10;
arr[term] = digit;
term++;
copy/=10;
}
for(int i = 0;i<nd-1;i++)
{
for(int j = i+1;j<nd;j++)
{
if(arr[i] == arr[j])
return false;
}
}
return true;
}
void main() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the value of n to find all n digit unique nos - ");
long n = Long.parseLong(br.readLine());
long count = 0;
for(long i = (long)Math.pow(10,n-1);i<=(long)Math.pow(10,n)-1;i++)
{
if(checkUnique(i)==true)
{
System.out.println(i);
count++;
}
}
System.out.println(" "+count+" unique nos have been generated.");
}
}

Prg for omit common character from a string in java

class wihoutcommon
 {
     void find(String st)
     {
         int i,j,k,m, flag,f=0;
          char ch,ch1,ch2;
           String wrd="";
          i=st.length();
          for(j=0;j<i;j++)
          {flag=0;
              ch=st.charAt(j);
              for(k=0;k<j;k++)
              {
                  if(ch==st.charAt(k))
                  {
                      flag=1;
                      break;
                    }
                }
                if(flag==0)
                {
                    for(m=j+1;m<i;m++)
                    {f=0;
                        if(ch==st.charAt(m))
                        {
                            f=1;
                            break;
                        }
                    }
                 if(f==0)  
                wrd+=ch;
            }
        }
            System.out.println(wrd);
       
    }
}          

Prog for display frequency of each word fr a sentence in java

import java.io.*;
class word_freq
   {
       public void main()throws IOException
       {
           BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
           System.out.println("Enter the sentence");
           String st=br.readLine();
           int i,j,k,count=0,c=0,m,n,flag=0;
           char ch;
           String wrd="";
           st=st+" ";
           i=st.length();
           for(j=0;j<i;j++)
           {
               ch=st.charAt(j);
               if(ch==' ')
               count++;
            }
            String arr[]=new String[count];
            int pos=0;
            for(j=0;j<i;j++)
            {
                ch=st.charAt(j);
                if(ch!=' ')
                wrd+=ch;
                else
                {
                arr[pos++]=wrd;
                wrd="";
            }
          
            }
            for(m=0;m<pos;m++)
            {
                flag=0;
                for(n=0;n<m;n++)
                {
                    if(arr[m].compareTo(arr[n]) == 0)
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==0)
                {
                c=1;
                for(k=m+1;k<pos;k++)
                { 
                    if(arr[m].compareTo(arr[k]) == 0)
                    c++;
                    }
                    System.out.println(arr[m]+" => "+c);
                }
            }
        }
    }
               
               
               
           

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;
                    }
                }