Friday, October 21, 2011

Buzz Number or GCD

// CHECK WHETHER A NUMBER IS A BUZZ NUMBER OR A GCD NUMBER
// VARIABLE DESCRIPTION:
// last=STORES THE LAST CHARACTER
// gcd1=GCD NUMBER 1
// gcd2=GCD NUMBER 2
// check=input
// number=stores the number
// denomintaor=stores the denominator
import java.io.*;
class BUZZ_OR_GCD
{
    public static void main(String args[])throws IOException
    {
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader input = new BufferedReader(reader);
        System.out.println("1> to check buzz number(buzz number is a number ending with 7 or is divisible by 7)");
        System.out.println("2> to check GCD(greatest common divisor).");
        int select = Integer.parseInt(input.readLine());/**variable to control the switch & also stores user input.*/
        switch(select)
        {
            case 1:
            {
                System.out.println("Enter a whole number whose Buzz numbe has to be checked.");
               int check = Integer.parseInt(input.readLine());
               int last= check%10;
               if(last==7||last%7==0)
               System.out.println("The number is a buzz number.");
               else
               System.out.println("The number is not a buzz number.");
               break;
            }
            case 2:
            {
                System.out.println("Enter a number whose GCD has to be checked.");
                int gcd1 = Integer.parseInt(input.readLine());
                System.out.println("Enter another number whose GCD has to be checked.");
                int gcd2 = Integer.parseInt(input.readLine());
                int number;
                int denominator;
                int GCD = 0,oper = 0;
                if(gcd1>gcd2)
                {
                    number = gcd1;
                    denominator = gcd2;
                }
                else
                {
                    number = gcd2;
                    denominator = gcd1;
                }
                while(denominator>1)
                {
                    oper = number%denominator;
                    if(oper==0)
                    {
                        GCD = denominator;
                        break;
                    }
                    else
                    {
                        number = denominator;
                        denominator = oper;
                    }
                }
                if(denominator==1)
                    GCD = 1;
                    System.out.println("The GCD of "+gcd1+" and "+gcd2+" is: "+GCD);
                }
                default:
                System.out.println("ENTERED WRONG DATA");
            }
        }
    }

                
                    OUTPUT 1:
1> to check buzz number(buzz number is a number ending with 7 or is divisible by 7)

2> to check GCD(greatest common divisor).

1
Enter a whole number whose Buzz numbe has to be checked.

67
The number is a buzz number.



                    OUTPUT 2:
1> to check buzz number(buzz number is a number ending with 7 or is divisible by 7)

2> to check GCD(greatest common divisor).

1
Enter a whole number whose Buzz numbe has to be checked.

45
The number is not a buzz number.

                    OUTPUT 3:
1> to check buzz number(buzz number is a number ending with 7 or is divisible by 7)

2> to check GCD(greatest common divisor).

2
Enter a number whose GCD has to be checked.

2
Enter another number whose GCD has to be checked.

8
The GCD of 2 and 8 is: 2



                    OUTPUT 4:
1> to check buzz number(buzz number is a number ending with 7 or is divisible by 7)

2> to check GCD(greatest common divisor).

5
ENTERED WRONG DATA





 

No comments:

Post a Comment