pc-ip-15.c (893B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 05 Jan 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* 9 * Question 15: 10 * Write a program to calculate the GCD of two numbers using recursive and iterative function. 11 */ 12 13 #include <stdio.h> 14 15 int gcd_rec(int, int); 16 int gcd_ite(int, int); 17 18 int main() 19 { 20 int a, b; 21 printf("Enter two number: "); 22 scanf("%d %d", &a, &b); 23 printf("\nGCD(%d, %d) (Recursion) = %d", a, b, gcd_rec(a, b)); 24 printf("\nGCD(%d, %d) (Iteration) = %d", a, b, gcd_ite(a, b)); 25 return 0; 26 } 27 28 int gcd_ite(int a, int b) 29 { 30 int temp; 31 while (a != 0) 32 { 33 temp = a; 34 a = b % a; 35 b = temp; 36 } 37 return b; 38 } 39 40 int gcd_rec(int a, int b) 41 { 42 if (a == 0) 43 { 44 return b; 45 } 46 else 47 { 48 return gcd_rec(b % a, a); 49 } 50 }