-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGcdLcmArray.cpp
More file actions
50 lines (46 loc) · 811 Bytes
/
GcdLcmArray.cpp
File metadata and controls
50 lines (46 loc) · 811 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
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;
int gcd(int a,int b)
{
if(a>b){
int t;
t=a;
a=b;
b=t;
}
if(a==0)
return b;
return gcd(b%a,a);
}
int gcdArray(int arr[],int l)
{
int result=arr[0];
for(int i=1;i<l;i++)
{
result=gcd(arr[i],result);
if(result==1)
return 1;
}
return result;
}
int lcmArray(int arr[],int l)
{
int result=arr[0];
for(int i=1;i<l;i++)
{
result=(arr[i]*result)/gcd(arr[i],result);
}
return result;
}
int main()
{
int arr[10];
int n;
cout<<"Enter Length:";
cin>>n;
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<gcdArray(arr,n)<<endl;
cout<<lcmArray(arr,n)<<endl;
return 0;
}