Write a menu driven program in java to accept a number from the user and check whether it is 'BUZZ' number or to accept any two numbers and print their ' GCD'.
Answers
Answer:
Write a menu driven program to accept a number from the user and check whether it is a BUZZ number or to accept any two number and print the GCD of them.
A BUZZ number is the number which either ends with 7 or is divisible by 7.
GCD (Greatest Common Divisor) of two integers is calculated by continued division method. Divide the largest number by the smaller ; the remainder then divides the precious divisor. The process is repeated till the remainder is zero.The divisor then results the GCD.[ISCE 2009]
Solution
import java.util.*;
public class Number
{
public static int gcd (int a, int b) {
int num, den, GCD = 0, r ;
if (a > b) {
num = a ;
den = b ;
}
else {
num = b ;
den = a ;
}
while(den > 1)
{ r = num % den ;
if(r == 0) {
GCD = den ;
break ;
}
else {
num = den ;
den = r ;
}
}
if (den == 1)
GCD = 1 ;
return GCD ;
}
public static boolean BUZZ ( int a ){
if ( a % 7 ==0 || a % 10 == 7) return true ;
else return false ;
}
public static void main(String[ ] args ) {
Scanner kb = new Scanner( System.in );
System.out.println("Menu");
System.out.println("1. BUZZ number");
System.out.println("2. GCD of two numbers");
System.out.println("Enter your choice ");
int ch = kb.nextInt();
if ( ch ==1 ){
System.out.println("Enter a number");
int num = kb.nextInt();
if (BUZZ(num) == true)
System.out.println(num+" is a BUZZ number");
else
System.out.println(num+" is not a BUZZ number");
}
else if ( ch ==2 ){
System.out.println("Enter two numbers ");
int num1 = kb.nextInt();
int num2 = kb.nextInt();
int res = gcd(num1, num2);
System.out.println("GCD of "+num1+" and "+num2+" : "+res);
}
else
System.out.println("Wrong Choice!!");}
}
Answer:
class questionEIGHT2009
{
public static void main(String args[]) throws IOException
{
int i,j;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("**************MENU*************");
System.out.println("Type 1 to check if a number is a BUZZ number or not");
System.out.println("Type 2 to print GCD of any two numbers");
System.out.println("Enter your choice");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter the number to check if it is buzz number or number");
int n=Integer.parseInt(br.readLine());
if(n%7==0 || n%10==7)
{
System.out.println("The number " +n+ " is a BUZZ number");
}
else
{
System.out.println("The number " +n+ " is NOT a BUZZ number");
}
break;
case 2:
System.out.println("Enter any two numbers to print their GCD");
int n1=Integer.parseInt(br.readLine());
int n2=Integer.parseInt(br.readLine());
int divisor, dividend;
if(n1>n2)
{
dividend=n1;
divisor=n2;
}
else
{
dividend=n2;
divisor=n1;
}
int rem=1;
while(rem!=0)
{
rem=dividend%divisor;
if(rem==0)
{
System.out.println("GCD is = " +divisor);
}
else
{
dividend=divisor;
divisor=rem;
}
}
break;
default:
System.out.println("Wrong choice");
}
}
}