forked from AllAlgorithms/cpp
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcm_of_array.cpp
More file actions
Latest commit
42 lines (33 loc) · 706 Bytes
/
Copy pathlcm_of_array.cpp
File metadata and controls
42 lines (33 loc) · 706 Bytes
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
// C++ program to find LCM of n elements
// Author Bharat Reddy
#include<bits/stdc++.h>
usingnamespacestd;
typedeflonglongint ll;
intgcd(int a, int b)
{
if (b == 0)
return a;
returngcd(b, a % b);
}
// Returns LCM of array elements
ll findlcm(int arr[], int n)
{
ll ans = arr[0];
for (int i = 1; i < n; i++)
ans = (((arr[i] * ans)) /
(gcd(arr[i], ans)));
return ans;
}
intmain()
{
int n;
cout<<"Enter size of array : ";
cin>>n;
int a[n];
cout<<"Enter elements of array"<<endl;
int i;
for(i=0;i<n;i++)
cin>>a[i];
printf("%lld\n", findlcm(a, n));
return0;
}