Question : Given an array of n numbers, find LCM of it.
Since LCM(a,b) = a*b / GCD(a,b), here is my original code:
class GFG {
// the simple 2 integers recursive to find their GCD.
static int getGcd(int a, int b){
while(a != b){
if(a >b){
a = a-b;
}else{
b= b-a;
}
}
return a;
}
// find GCD in an array that has more than two integers.
static int Gcd(int[] newArray){
int gcd = newArray[0];
for(int i=1; i<newArray.length; i++){
gcd = getGcd(gcd,newArray[i]);
}
return gcd;
}
static int Lcm(int[] newArray){
int multi =1;
for(int i=0; i< newArray.length; i++){
multi = multi* newArray[i];
}
return (multi/gcd);
}
public static void main (String[] args) {
int[] newArray = { 2, 7, 3, 9, 4 };
System.out.println(Gcd(newArray));
System.out.println(Lcm(newArray));
}
}
But when I run this code, it shows some error:
prog.java:33: error: cannot find symbol
return (multi/gcd);
^
symbol: variable gcd
I don't know how to fix it. Please help me to correct my code... Thanks!!!