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


No comments:

Post a Comment