Hi All,
import java.util.Scanner;
//main class
public class GCD_LCD {
private static int lcm;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number1: ");
int num1 = input.nextInt();
System.out.print("Enter the number2: ");
int num2 = input.nextInt();
// for GCD
if (num1 > num2) {
System.out.println("the GCD of " + num1 + " and " + num2 + " is " + GCD(num1, num2));
} else {
System.out.println("the GCD of " + num1 + " and " + num2 + " is " + GCD(num1, num2));
}
//For LCD
LCD(num1, num2);
}
public static int GCD(int x, int y) {
int result;
if (y == 0) {
return x;
}
result = GCD(y, x % y);
return result;
}
public static int LCD(int x, int y) {
for (int i = 1; i <= x * y; i++) {
if (i % x == 0 && i % y == 0) {
lcm = i;
break;
}
}
System.out.println("the LCD of " + x + " and " + y + " is " + lcm);
return 0;
}
}
Enter the number1: 30
Enter the number2: 72
the GCD of 30 and 72 is 6
the LCD of 30 and 72 is 360
The greatest common divisor (gcd), also known as the greatest common factor (gcf), or highest common factor (hcf).of two or more integers (at least one of which is not zero), is the largest positive integer that divides the numbers without a remainder.
The least common multiple is the smallest positive integer that is a multiple of both the numbers.
import java.util.Scanner;
//main class
public class GCD_LCD {
private static int lcm;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number1: ");
int num1 = input.nextInt();
System.out.print("Enter the number2: ");
int num2 = input.nextInt();
// for GCD
if (num1 > num2) {
System.out.println("the GCD of " + num1 + " and " + num2 + " is " + GCD(num1, num2));
} else {
System.out.println("the GCD of " + num1 + " and " + num2 + " is " + GCD(num1, num2));
}
//For LCD
LCD(num1, num2);
}
public static int GCD(int x, int y) {
int result;
if (y == 0) {
return x;
}
result = GCD(y, x % y);
return result;
}
public static int LCD(int x, int y) {
for (int i = 1; i <= x * y; i++) {
if (i % x == 0 && i % y == 0) {
lcm = i;
break;
}
}
System.out.println("the LCD of " + x + " and " + y + " is " + lcm);
return 0;
}
}
output:
~~~~~
Enter the number1: 30
Enter the number2: 72
the GCD of 30 and 72 is 6
the LCD of 30 and 72 is 360
No comments:
Post a Comment