Computer Science, asked by Ayush0009, 11 months ago

Write a program in Java to accept two numbers using an input statement then calculate and display the GCD and LCM of the two numbers with proper message. Do this program fast. I want compiled one.

Answers

Answered by suhanisuryawanshi29
2
Yes its fast........
Answered by aqibkincsem
5

"Two Java programs accepting two numbers using an input statement and displaying the GCD and LCM of two numbers are as follows. (1) public class GCD { public static void main(String[] args) { int n1 = 81, n2 = 153, gcd = 1; for(int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if(n1 % i==0 && n2 % i==0) gcd = i; } System.out.printf(""G.C.D of %d and %d is %d"", n1, n2, gcd); } }

When you run the program, the output will be: G.C.D of 81 and 153 is 9. (2) public class LCM { public static void main(String[] args) { int n1 = 72, n2 = 120, lcm; // maximum number between n1 and n2 is stored in lcm lcm = (n1 > n2) ? n1 : n2; // Always true while(true) { if( lcm % n1 == 0 && lcm % n2 == 0 ) { System.out.printf(""The LCM of %d and %d is %d."", n1, n2, lcm); break; } ++lcm; } } }

When you run the program, the output will be: The LCM of 72 and 120 is 360."

Similar questions