Need help solving this I got a good chunk of it done usink a
Need help solving this.
I got a good chunk of it done usink an arrayList but i\'ve been stuck for hours.
write function that returns the LCM of a list of numbers. The function has 1 parameter - a list of integers and return a number - the LCM of the numbers in the list.Solution
import java.util.*;//importing util package becuase array list present in that package
public class Lcm {
public static void main(String args[]){
ArrayList<Integer> a=new ArrayList<Integer>();//defining teh arraylist
a.add(2); //adding elements to array list
a.add(4);
a.add(6);
System.out.println(findLcm(a)); //printing the lcm of given arrylist of elements
}
static int gcd(int a,int b){ //to find gcd of given numbers
if(b==0)
return a;
return gcd(b,a%b); //retuns the gcd of given numbers
}
static int findLcm(ArrayList<Integer> a){ //find the lcm of array of elements
int result=1;
for(Integer i : a){ //i loops through arrya
result=result*i/gcd(result,i); //caluculates the lcm of given array
}
return result; //return the result
}
}
