Friday, June 8, 2012

Write a program to check all the math class functions.?


                           ♣♣♣ Write a program to check all the math class functions. ♣♣♣

import java.io.*;
import java.util.*;
import java.lang.*;
class pro9
{
            public static void main(String args[]) throws IOException
            {
                        InputStreamReader a =new InputStreamReader(System.in);
                        BufferedReader b =new BufferedReader(a);
                        Stack st=new Stack();
                        char ans='y';
                        while(ans=='y' || ans=='Y')
                        {
                        System.out.println("\n\tMENU FOR STACK OPERATION");
                        System.out.println("\n\t1.\t PUSH");
                        System.out.println("\n\t2.\t POP");
                        System.out.println("\n\t3.\t PEEK");
                        System.out.println("\n\t4.\t SEARCH");
                        System.out.println("\n\t5.\t DISPLAY");
                        System.out.println("\n\t6.\t EXIT");
                        System.out.print("\n\nEnter your choice : ");
                        int ch=Integer.parseInt(b.readLine());
                        switch(ch)
                        {
                                    case 1:
                                                System.out.print("Enter the total number of element to be push : ");
                                                int n=Integer.parseInt(b.readLine());
                                                for(int i=1;i<=n;i++)
                                                {
                                                            System.out.print("\n\tEnter the "+i+" Element : ");
                                                            int val=Integer.parseInt(b.readLine());
                                                            st.push(new Integer(val));
                                                }
                                                break;
                                    case 2:
                                                try
                                                {
                                                System.out.print("\n\tElement poped is  "+st.pop());
                                                }
                                                catch(Exception e)
                                                {
                                                System.out.println("\n\tStack is underflow........");
                                                }
                                                break;
                        case 3:
                                    try
                                    {
                            System.out.println("\n\tElement on the top of the Stack is "+(Integer)st.peek());
                                    }
                                    catch(Exception e)
                                    {
                                    System.out.print("\n\tStack is Empty");
                                    }
                                    break;
                        case 4:
                                    System.out.print("\n\tEnter the element to be searched : ");
                                    int num=Integer.parseInt(b.readLine());
                                    int sr=st.search(new Integer(num));
                                    if(sr==-1)
                                    System.out.println("\nElemet does not found in the stack");
                                    else
                                    System.out.println("\nElement found in the Stack at "+sr+"position");
                                    break;
                        case 5:
                                    System.out.println("Elemets of the stack are ");
                                    System.out.println(st);
                                    break;
                        case 6:
                                    System.exit(0);
                        default:
                                    System.out.print("Invalid Choice.......Please enter the correct choice");
                        }
                        System.out.print("Do you want to continue (Y/N) : ");
                        String s=b.readLine();
                        ans=s.charAt(0);
                        }
            }
}

OUTPUT :-

            MENU FOR STACK OPERATION
1.                                          PUSH
2.                                          POP
3.                                          PEEK
4.                                          SEARCH
5.                                          DISPLAY
6.                                          EXIT

Enter your choice : 1
Enter the total number of element to be push : 2
            Enter the 1 Element : 23
            Enter the 2 Element : 45
Do you want to continue(Y/N) : y

MENU FOR STACK OPERATION
1.                                          PUSH
2.                                          POP
3.                                          PEEK
4.                                          SEARCH
5.                                          DISPLAY
6.                                          EXIT

Enter your choice : 4
Enter the total element to be searched : 55
Element does not found in the stack
Do you want to continue(Y/N) : y



MENU FOR STACK OPERATION
1.                                          PUSH
2.                                          POP
3.                                          PEEK
4.                                          SEARCH
5.                                          DISPLAY
6.                                          EXIT

Enter your choice :5
            Elements of the stack are
            [23, 45]
Do you want to continue(Y/N) : n


Write a program to create object for TreeSet and Stack and use all methods.


♣♣♣ WRITE A PROGRAM TO CREATE OBJECT FOR TREESET AND STACK AND USE ALL METHODS. ♣♣♣


import java.io.*;
import java.util.*;
class pro8
{
            public static void main(String args[]) throws IOException
            {
                        InputStreamReader a =new InputStreamReader(System.in);
                        BufferedReader b =new BufferedReader(a);
                        TreeSet ts=new TreeSet();
                        System.out.print("\nEnter the total no. of Items : ");
                        int n=Integer.parseInt(b.readLine());
                        String str=null;
                        for(int cnt=1;cnt<=n;cnt++)
                        {
                                    System.out.print("\nEnter the "+cnt+" character : ");
                                    str=b.readLine();
                                    ts.add(str);
                        }
                        System.out.println("\n\tThe Item on the tree are : \t"+ts);
                        System.out.println("\n\tThe first item on the tree is : \t"+ts.first());
                        System.out.println("\n\tThe last item on the tree is : \t"+ts.last());
                        System.out.println("\n\tTotal no. of item on the tree are : \t"+ts.size());
                        System.out.print("\nEnter the element you want to delete : ");
                        String del=b.readLine();
                        ts.remove(del);
                        System.out.println("\n\tThe Item on the tree are : \t"+ts);
            }
}

OUTPUT :-

            Enter the total no. of Items :4
Enter the 1 character : we
Enter the 2 character : are
Enter the 3 character : LogicCafe 
Enter the 4 character : Team

The Item on the tree are :                             [are,  LogicCafe , Team, we]
The first item on the tree is :            are
The last item on the tree is : we
Total no. of item on the tree are :    4
Enter the element you want to delete :  we
The Item on the tree are : [are,  LogicCafe , Team]

Write a program to sort existing array?


♣♣♣ WRITE A PROGRAM TO SORT EXISTING ARRAY. ♣♣♣


import java.io.*;
class pro7
{
            public static void main(String args[]) throws IOException
            {
            int cnt,last,i,j,temp;
            int arr[]=new int[10];
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter the total number of array : ");
            last=Integer.parseInt(b.readLine());
                        for(cnt=0;cnt
                        {         
                        System.out.print("Enter element"+(cnt+1)+" : ");
                        arr[cnt]=Integer.parseInt(b.readLine());        
                        }
                        for(i=1;i<=last;i++)
                        {
                                    for(j=1;j<=last;j++)
                                    {
                                                if(arr[j-1]
                                                {
                                                temp=arr[j-1];
                                                arr[j-1]=arr[j];
                                                arr[j]=temp;
                                                }
                                    }
                        }
                        System.out.println("Array after sorting is :");
                        for(cnt=0;cnt
                        {
                        System.out.print(arr[cnt]+"   ");         
                        }         
            }
}

OUTPUT :-

            Enter the total number of array : 5
Enter Element1 :23
Enter Element2 :12
Enter Element3 :45
Enter Element4 :33
Enter Element5 :21

Array after sorting is :
            45        33        23        21        12

Write a program to insert element in existing array.?


♣♣♣ WRITE A PROGRAM TO INSERT ELEMENT IN EXISTING ARRAY. ♣♣♣

import java.io.*;
class pro6
{
            public static void main(String args[]) throws IOException
            {
            int cnt,last,pos,num;
            int arr[]=new int[10];
            int newarr[]=new int[10];
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter the total number of array : ");
            last=Integer.parseInt(b.readLine());
                        for(cnt=0;cnt
                        {         
                        System.out.print("Enter element"+(cnt+1)+" : ");
                        arr[cnt]=Integer.parseInt(b.readLine());        
                        }
                     System.out.print("Enter the position to insert new element in an existing array :");
                        pos=Integer.parseInt(b.readLine());
                        System.out.print("Enter the element : ");
                        num=Integer.parseInt(b.readLine());
                        for(cnt=0;cnt
                        {         
                                    if(cnt+1==pos)
                                    newarr[cnt]=num;
                                    else
                                    newarr[cnt]=arr[cnt];
                        }
                        System.out.println("Array after inserting new element is :");
                        for(cnt=0;cnt
                        {
                        System.out.print(newarr[cnt]+"   ");  
                        }         
            }
}
OUTPUT :-

            Enter the total number of array : 5
Enter Element1 :23
Enter Element2 :12
Enter Element3 :45
Enter Element4 :33
Enter Element5 :21
Enter the position to insert new element in an existing array :2
Enter the element : 88
Array after inserting new element is :
            23        88        45        33        21

Write a program to display al?l prime numbers between 1 to 1000.


♣♣♣ WRITE A PROGRAM TO DISPLAY ALL PRIME NUMBERS BETWEEN 1 TO 1000. ♣♣♣


import java.io.*;
class pro5
{
            public static void main(String args[]) throws IOException
            {
            int num,cnt,last,flg;
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter the last number of range: ");
            last=Integer.parseInt(b.readLine());
            System.out.print("List of all Prime Number upto "+last+" : \n");
                        for(num=1;num<=last;num++)
                        {
                        flg=1;
                                    for(cnt=2;cnt<=num/2;cnt++)
                                    {         
                                                if(num%cnt==0)
                                                {
                                                flg=0;
                                                break;
                                                }
                                    }
                        if(flg==1)
                        System.out.print(num+"   ");
                        }
            }
}

  

OUTPUT :-

            Enter the last number of range: 50

List of all Prime Number upto 50:

            1          2          3          5          7          11        13        17        19        23
           29         31        37       41       43          47

WRITE A PROGRAM TO DISPLAY MULTIPLICATION TABLE.?


♣♣♣ WRITE A PROGRAM TO DISPLAY MULTIPLICATION TABLE. ♣♣♣

import java.io.*;
class pro4
{
            public static void main(String args[]) throws IOException
            {
            int num,cnt,sum;
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter any number : ");
            num=Integer.parseInt(b.readLine());
                        for(cnt=1;cnt<=10;cnt++)
                        {
                        sum=num*cnt;
                        System.out.println(num+" * "+cnt+" = "+ sum);
                        }
            }
}
  
OUTPUT :-

            Enter any number : 3
            3  *  1  =  3
3  *  2  =  6
            3  *  3  =  9
            3  *  4  =  12
            3  *  5  =  15
3  *  6  =  18
            3  *  7  =  21
            3  *  8  =  24
            3  *  9  =  27
3  *  10  =  30

WRITE A PROGRAM TO FIND THE SUM OF DIGITS OF A GIVEN NUMBER.?


♣♣ WRITE A PROGRAM TO FIND THE SUM OF DIGITS OF A GIVEN NUMBER. ♣♣



import java.io.*;
class pro3
{
            public static void main(String args[]) throws IOException
            {
            int num,rem,sum=0;
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter any numeric number : ");
            num=Integer.parseInt(b.readLine());
                        while(num!=0)
                        {
                        rem=num%10;
                        sum=sum+rem;
                        num=num/10;
                        }
            System.out.print("Sum of Number= "+ sum);
            }
}


OUTPUT :-

            Enter any numeric  number : 4652
            Sum of Number = 17