Write Java program Lab42.java which takes as input two positive integers m and n and computes their least common multiple by calling method lcm(m,n), which in turn calls recursive method gcd(m,n) computing the greatest common divisor of m and n. Recall, that lcm(m,n) can be defined as quotient of m * n and gcd(m,n).
//Lab42.java import java.util.Scanner; public class Lab42{ public static int gcd(int m, int n){ if (n != 0) return gcd(n, m%n); else return m; } public static int lcm(int m, int n){ return (m*n)/gcd(m,n); } public static void main(String args[]) { int num1, num2; Scanner in = new Scanner(System.in); System.out.print("Enter number1: "); num1 = in.nextInt(); System.out.print("Enter number2: "); num2 = in.nextInt(); System.out.println("LCM = "+lcm(num1,num2)); } }
Get Answers For Free
Most questions answered within 1 hours.