Program to display Greatest Common Divisor (GCD) of two numbers
Program to display Greatest Common Divisor (GCD) of two numbers
1 |
/*Program to display Greatest Common Divisor (GCD) of two numbers*/ |
2 |
#include <stdio.h> |
3 |
#include <conio.h> |
4 |
int divisor(int num1,int num2,int div) |
5 |
{ |
6 |
static int max=0; |
7 |
if(div<num1 && div<num2) |
8 |
{ |
9 |
if(num1%div == 0 && num2%div == 0) |
10 |
{ |
11 |
if(div>max) |
12 |
{ |
13 |
max=div; |
14 |
divisor(num1,num2,div+1); |
15 |
} |
16 |
} |
17 |
else |
18 |
{ |
19 |
divisor(num1,num2,div+1); |
20 |
} |
21 |
} |
22 |
else |
23 |
{ |
24 |
return(max); |
25 |
} |
26 |
return(max); |
27 |
} |
28 |
void main() |
29 |
{ |
30 |
int num1,num2,div; |
31 |
printf("Enter two number to find it's divisors(o to exit): "); |
32 |
scanf("%d %d",&num1,&num2); |
33 |
div=divisor(num1,num2,1); |
34 |
printf("Greatest Common divisor of %d and %d is: %d",num1,num2,div); |
35 |
getch(); |
36 |
} |
37 |
|