-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion12.c
63 lines (47 loc) · 1.35 KB
/
question12.c
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
51
52
53
54
55
56
57
/*
2. Write a C Program to find the Greatest Common Divisor (GCD) of two positive integers,
using a function "int GCD (int a, int b)" which returns the GCD of two
positive integers "a" and "b" passed as arguments. Note that the values of "a" and "b" might be
arbitrary, and proper arrangements should be made to ensure that the
function works for both the cases: "a >= b" and "a < b".
Input : two positive integers in the "main()" portion of the program.
Output: the GCD, as returned by the "GCD()" function. [2]
*/
#include <stdio.h>
int GCD(int a, int b){
int min, value;
min = (a < b)?a:b;
if(a%min == 0 && b%min == 0){
value = min;
}else{
for(int i= (min-1); i>1;i--){
if(a%i ==0 && b%i ==0){
value = i;
break;
}
}
}
return value;
}
int main(){
int num1, num2, ans;
printf("Enter two positive integers:\n");
scanf("%i %i", &num1, &num2);
ans = GCD(num1, num2);
printf("GCD for %d and %d is %d\n",num1, num2, ans);
}
//Alternate way
// #include <stdio.h>
// int main(){
// int n1, n2;
// printf("Enter two positive integers: ");
// scanf("%d %d",&n1,&n2);
// while(n1!=n2){
// if(n1 > n2)
// n1 -= n2;
// else
// n2 -= n1;
// }
// printf("GCD = %d",n1);
// return 0;
// }