-
Notifications
You must be signed in to change notification settings - Fork 3
/
GCD.cpp
48 lines (36 loc) · 1.28 KB
/
GCD.cpp
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
//Including required streaming library
#include <iostream>
//Using standard namespace for whole code
using namespace std;
//making a function to perform the gcd and returning the value
int gcd(int n , int a , int b){ //Passed arguments are for iteration and calculations
//initialized and declared some variables
int max = 0;
int c,d;
//Using for loop to iterate till the greatest input by user
for (int i = 1 ; i < n ; ++i){
c = a % i;
d = b % i;
if (c == 0 && d == 0){
//changing value of max when it is a common divisor
max = i;
}
}
//returning the GCD
return max;
}
//Entry level for execution
int main(){
//Declaring some variables to take inputs
int a,b,n;
cout<<"Enter two number to get GCD of : \n";
cin>>a>>b;
//Setting "n" to greatest input by user
n = (a>=b)? a : b;
int max =0 ;
//Finalising the code
max = gcd(n,a,b);
cout<<max;
//Helping compiler to know it ran Successfully
return 0;
}