pc-ip-15.c (702B)
1 /* 2 * Question 15: 3 * Write a program to calculate the GCD of two numbers using recursive and iterative function. 4 */ 5 6 #include <stdio.h> 7 8 int gcd_rec(int, int); 9 int gcd_ite(int, int); 10 11 int main() 12 { 13 int a, b; 14 printf("Enter two number: "); 15 scanf("%d %d", &a, &b); 16 printf("\nGCD(%d, %d) (Recursion) = %d", a, b, gcd_rec(a, b)); 17 printf("\nGCD(%d, %d) (Iteration) = %d", a, b, gcd_ite(a, b)); 18 return 0; 19 } 20 21 int gcd_ite(int a, int b) 22 { 23 int temp; 24 while (a != 0) 25 { 26 temp = a; 27 a = b % a; 28 b = temp; 29 } 30 return b; 31 } 32 33 int gcd_rec(int a, int b) 34 { 35 if (a == 0) 36 { 37 return b; 38 } 39 else 40 { 41 return gcd_rec(b % a, a); 42 } 43 }