-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCMOfTwoNumbers.java
More file actions
61 lines (43 loc) · 1.12 KB
/
LCMOfTwoNumbers.java
File metadata and controls
61 lines (43 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package numerics;
import java.util.Scanner;
public class LCMOfTwoNumbers {
public static void lcmUsingGCD(int num1,int num2)
{
int gcd = 0,lcm;
for (int i = 1; i <= num1 && i <= num2; ++i) {
if (num1 % i == 0 && num2 % i == 0)
gcd = i;
}
lcm = (num1 * num2) / gcd;
System.out.println("The LCM of "+num1+" and "+num2+" Using GCD is :"+ lcm);
}
public static void lcmUsingWhileLoop(int num1,int num2)
{
int min = (num1 > num2) ? num1 : num2;
while (true) {
if (min % num1 == 0 && min % num2 == 0) {
System.out.println("\nThe LCM of "+num1+" and "+num2+" Using WHILE Loop is :"+ min);
break;
}
++min;
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num1,num2;
System.out.println("Enter Two Positive Integers : ");
num1 = sc.nextInt();
num2 = sc.nextInt();
lcmUsingGCD(num1,num2);
lcmUsingWhileLoop(num1,num2);
}
}
/*
Output :
Enter Two Positive Integers :
72
120
The LCM of 72 and 120 Using GCD is :360
The LCM of 72 and 120 Using WHILE Loop is :360
*/