Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.*; import java.math.RoundingMode; import java.text.DecimalFormat; class Easy { // Recursive function to return gcd of a and b public static int calcGCD(int a, int

import java.util.*;

import java.math.RoundingMode;

import java.text.DecimalFormat;

class Easy

{

// Recursive function to return gcd of a and b

public static int calcGCD(int a, int b)

{

if (b == 0)

return a;

return calcGCD(b, a % b);

}

public static void main(String[] args)

{

Scanner sc=new Scanner(System.in);

DecimalFormat df = new DecimalFormat("0.00");

char opt='#';int n;

while(opt!='0')

{

int correct=0,wrong=0;

System.out.println(" 1) Quotient 2) Least Common Multiple (LCM) 3) Greatest Common Multiple (GCD) 0) Quit");

opt=sc.next().charAt(0);

switch(opt)

{

case '1':

System.out.print("Number Of Question : ");

n=sc.nextInt();

for(int i=1;i

{

//generate random numbers between [0,150]

int num1 = (int)(Math.random() * (150 - 1 + 1) + 0);

//generate random numbers between [1,40]

int num2 = (int)(Math.random() * (40 - 1 + 1) + 1);

System.out.print("Q"+i+": "+num1+"/"+num2+" = ");

int ans=sc.nextInt();

int correctans=num1um2;

if(ans==correctans)

correct++;

else

wrong++;

}

System.out.println("You have entered "+n+" questions "+correct+" of them are correct. And your mark is "+df.format((double)(correct*100.0))+"%");

break;

case '2':

System.out.print("Number Of Questions : ");

n=sc.nextInt();

for(int i=1;i

{

//generate random numbers between [1,80]

int num1 = (int)(Math.random() * (80 - 1 + 1) + 1);

//generate random numbers between [2,30]

int num2 = (int)(Math.random() * (30 - 1 + 1) + 2);

System.out.print("Q"+i+": LCM ("+num1+","+num2+") = ");

int ans=sc.nextInt();

int correctans=(num1*num2)/calcGCD(num1,num2);

if(ans==correctans)

correct++;

else

wrong++;

}

System.out.println("You have entered "+n+" questions "+correct+" of them are correct. And your mark is "+df.format((double)(correct*100.0))+"%");

break;

case '3':

System.out.print("Number Of Questions : ");

n=sc.nextInt();

for(int i=1;i

{

//generate random numbers between [1,200]

int num1 = (int)(Math.random() * (200 - 1 + 1) + 1);

int num2 = (int)(Math.random() * (200 - 1 + 1) + 1);

System.out.print("Q"+i+": GCD ("+num1+","+num2+") = ");

int ans=sc.nextInt();

int correctans=calcGCD(num1,num2);

if(ans==correctans)

correct++;

else

wrong++;

}

System.out.println("You have entered "+n+" questions "+correct+" of them are correct. And your mark is "+df.format((double)(correct*100.0))+"%");

break;

case '0':

System.out.println("End of Quiz!!");

break;

default:

System.out.println("Invalid Input");

break;

}

}

}

}

image text in transcribedimage text in transcribedimage text in transcribed
L * * * Main Menu * * *4 1) Quotient+ 2) Least Common Multiple (LCM) + 3) Greatest Common Divisor (GCD) + 0} Quit+ >0+ End of Quiz! !+ Add two more choices in the main menu; they are Countprimeand Power. If Countprime is selected, Step 3 generates division questions. If Power is selected, Step 3 generates questions with random operators.+' Operands generated in Countprimeand Power should obey the following rules:+ Operator Operands+ COUNTPRIME+ First operand should be betweenl and 5.+ Second operand should be between6 and 20.+ POWER+ Please random a number C.+ if C is aneven number, please generate: + A10+ B'+C + If C is an odd number, please generate:+ A10_B'+C + A: should be between 1 and 2+ B: should be between 0 and 4+ C: should be between 101 and 1994 Following is a Test Run:+ * * * Main Menu * * *+ 1) Quotient+ 2) Least Common Multiple (LCM) + 3) Greatest Common Divisor (GCD) + 4) Count primes+ 5) Power+ 0) Quit+ Number of Questions : 4+ Q1: Number of primes between 4 and 18 , including bothnumbers ) = 5+ 04: Number of primes between 2 and 8 , including both numbers ) = 4+ You've answered 4 questions+ 4 of them are correct.+ And your mark is 100.00%+ > 5+ Number of Questions : 3+ Q1: 1 ^10 + 0^7 + 124 = 125+ Q2: 2 ^10 + 4^7 + 170 = 17578+ Q3: 1 ^10 - 107 + 159 = 161159+ You've answered 3 questions+ 2 of them are correct.+ And your mark is 66.67%+ * * * Main Menu * * *4 1) Quotient+ 2) Least Common Multiple (LCM) + 3) Greatest Common Divisor (GCD) + 4) Count primes+ 5) Power+ 0) Quit+ > 0+ End of Quiz+ This advanced feature requires you to handle user's invalid input as follow:+ 1. Display meaningful error message, and+ 2. Repeat the same question or menu again unti input is correct.+ This program may have the following input errors:+ InStep 1: Main Menu, users input non integers or input out of range+ 1) Quotient+ 2) Least Common Multiple (LCM) + 3) Greatest Common Divisor (GCD) +4) Count primes+ 5) Power+ 0) Quit+ >X+ Error! Input accept integer only. Input 1-5 or 9.# + 1) Quotient+ 2) Least Common Multiple (LCM) + 3) Greatest Common Divisor (GCD) + 4) Count primes+ 5) Power+ 0) Quit+ 31+ +1 In Step 2: Specify the number of questions, user input non integers.+ Number of Questions: Y+ Error! Input accept integer only.+ Number of Questions: +

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

In what state was George Washington born?

Answered: 1 week ago

Question

What year did the American civil war begin?

Answered: 1 week ago

Question

American colonies independent from the British rule ?

Answered: 1 week ago

Question

Who was the principles author of the declaration of independence?

Answered: 1 week ago