File tree Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change
1
+ // C++ program to demonstrate Basic Euclidean Algorithm
2
+ // Author Bharat Reddy
3
+
4
+ #include < bits/stdc++.h>
5
+ using namespace std ;
6
+
7
+
8
+ int gcd (int a, int b)
9
+ {
10
+ if (a == 0 )
11
+ return b;
12
+ return gcd (b % a, a);
13
+ }
14
+
15
+ int main ()
16
+ {
17
+ int a,b;
18
+ cout<<" Enter 2 numbers : " ;
19
+ cin>>a>>b;
20
+ int g_c_d = gcd (a,b);
21
+ cout<<" GCD is <<g_c_d<<endl;
22
+ return 0;
23
+ }
Original file line number Diff line number Diff line change
1
+ // C++ program to find LCM of n elements
2
+ // Author Bharat Reddy
3
+
4
+ #include < bits/stdc++.h>
5
+ using namespace std ;
6
+
7
+ typedef long long int ll;
8
+
9
+
10
+ int gcd (int a, int b)
11
+ {
12
+ if (b == 0 )
13
+ return a;
14
+ return gcd (b, a % b);
15
+ }
16
+
17
+ // Returns LCM of array elements
18
+ ll findlcm (int arr[], int n)
19
+ {
20
+
21
+ ll ans = arr[0 ];
22
+
23
+ for (int i = 1 ; i < n; i++)
24
+ ans = (((arr[i] * ans)) /
25
+ (gcd (arr[i], ans)));
26
+
27
+ return ans;
28
+ }
29
+
30
+ int main ()
31
+ {
32
+ int n;
33
+ cout<<" Enter size of array : " ;
34
+ cin>>n;
35
+ int a[n];
36
+ cout<<" Enter elements of array" <<endl;
37
+ int i;
38
+ for (i=0 ;i<n;i++)
39
+ cin>>a[i];
40
+ printf (" %lld\n " , findlcm (a, n));
41
+ return 0 ;
42
+ }
You can’t perform that action at this time.
0 commit comments