-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCM.java
More file actions
84 lines (81 loc) · 1.93 KB
/
Copy pathLCM.java
File metadata and controls
84 lines (81 loc) · 1.93 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package basicjavaprograms;
import java.util.Scanner;
public class LCM{
public static void main(String args[]) {
System.out.println("Enter the LCM numbers:");
Scanner obj = new Scanner(System.in);
String arr=obj.nextLine();
String[] arrStr = arr.split(" ");
int len=arrStr.length,gcd=0,product=1,count=len-1;
for(int i=0;i<len-1;i++) {}
int[] arrNum = new int[len];
int LCM=1;
for(int i=0;i<=len-1;i++) {
arrNum[i]=Integer.valueOf(arrStr[i]);
System.out.println("the Lcm numbers:"+arrNum[i]);
}
for(int i=0;i<=len-1;i++) {
if(i==0 && count!=0) {
gcd= GCD(arrNum[i],arrNum[i+1]);
LCM =(arrNum[i]*arrNum[i+1])/gcd;
count--;
continue;
}
if((i>0) && (i<len-1) &&count!=0) {
gcd= GCD(arrNum[i+1],LCM);
LCM =(LCM*arrNum[i+1])/gcd;
count--;
continue;
}
if((i>0) && (i==len-1) &&count!=0) {
gcd= GCD(arrNum[i+1],LCM);
LCM =(LCM*arrNum[i+1])/gcd;
count--;
continue;
}
System.out.println("the GCD of "+gcd);
//product/gcd;
System.out.println("the Lcm of "+LCM);
}
System.out.println("the Lcm numbers Length"+len);
}
public static int GCD(int a, int b) {
while(a>0 && b>0) {
if(a>b)
a = a%b;
else
b=b%a;
}
if(a!=0)
return a;
else
return b;
}
}
/*package javasampleprogramming;
import java.util.Scanner;
public class LCM {
public static void main(String args[]) {
System.out.println("Enter the first number");
Scanner obj = new Scanner(System.in);
int a= obj.nextInt();
System.out.println("Enter the second number");
int b= obj.nextInt();
System.out.println("the LCM of"+a+" "+b);
int gcd= GCD(a,b);
int LCM = (a*b)/gcd;
System.out.println("the LCM of"+a+" "+b+"is"+LCM);
}
public static int GCD(int a, int b) {
while(a>0 && b>0) {
if(a>b)
a = a%b;
else
b=b%a;
}
if(a!=0)
return a;
else
return b;
}
}*/