Search This Blog

Sunday, December 12, 2010

BCD addition & subtraction - Menu based program (switch case) taking input from user

import java.io.*;
class bcd
{

    void m()throws IOException
    {
        char c; // used to store 'y' or 'n' for continuing or terminating the program
        do
        {

            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

            System.out.println("\n \n *************** MENU ******************\n");

           
            System.out.println("Enter 1 to perform BCD Addition of Two Numbers\n ");
            System.out.println("Enter 2 to perform BCD Subtraction of Two Numbers\n");
            System.out.print(" Enter your choice : ");
            int y=Integer.parseInt(br.readLine());
            switch(y)

            {
               
                case 1: System.out.print("Enter First No. : ");
                int n=Integer.parseInt(br.readLine());
                System.out.print("Enter Second No. : ");
                int m=Integer.parseInt(br.readLine());

                int z = n+m;
               
                System.out.println(" \n\t\tBCD Addition : \n");
                disp(z);

                break;
           
                case 2: System.out.print("Enter First No. : ");
                int x=Integer.parseInt(br.readLine());
                System.out.print("Enter Second No. : ");
                int a=Integer.parseInt(br.readLine());

                int w = x-a;
               
                System.out.println(" \n\t\tBCD Subtraction : \n");
                disp(w);

                break;


                default : System.out.println("Invalid choice");
            } // closing of switch case
            System.out.print("\nDo you want to continue ? Press y/n :\t");
            c=(char)br.read();
        }
        while(c!='n'); // closing of do-while loop
    } // closing of m function
   
    void disp(int num) // method to display the results after BCD ADDITION & BCD SUBTRACTION
    {
        int x[]=new int[32]; // To represent 32 bits positive BCD numbers
        if(num>99999999 || num<0)
        {
            System.out.println("Number should only be of eight digits and it should be positive");
            return;
        }
        int rem,r;
        int y[]={0,0,0,0}; // array of size 4 taken to store a digit when converted into binary
        int z=0;

        System.out.print(num+" = ");
        while(num>0)
        {
            rem=num%10; //Extracting the digits from decimal number
            num=num/10;
            for(int i=0;i<4;i++)
            {
                r=rem%2; // Converting the extracted digit into Binary
                rem=rem/2;
                y[i]=r;
            }
            for(int j=0;j<4;j++)
            {
                x[z]=y[j];

                z++;
            }
        }

        for(int j=31;j>=0;j--)
        {
            System.out.print(x[j]);
            if(j%4==0)
                System.out.print(" ");
        } // closing of for loop

       
    } // closing of disp method

   
} // closing of class


 OUTPUT:


 *************** MENU ******************

Enter 1 to perform BCD Addition of Two Numbers

Enter 2 to perform BCD Subtraction of Two Numbers

 Enter your choice : 1
Enter First No. : 15
Enter Second No. : 1

        BCD Addition :

16 = 0000 0000 0000 0000 0000 0000 0001 0110
Do you want to continue ? Press y/n :    n

No comments:

Post a Comment