assignment-s-19.c (1226B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 22 Dec 2025 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Write a program to calculate the GCD of two numbers 9 (i) using recursion 10 (ii) without recursion 11 */ 12 13 #include <stdio.h> 14 15 int gcd_tail_rec(int, int); 16 int gcd_rec(int, int); 17 int gcd_ite(int, int); 18 19 int main() 20 { 21 int a, b; 22 printf("Enter two number: "); 23 scanf("%d %d", &a, &b); 24 if (a < 0) 25 a = -a; 26 if (b < 0) 27 b = -b; 28 printf("\nGCD (Tail-Recursion) of %d and %d is = %d", a, b, gcd_tail_rec(a, b)); 29 printf("\nGCD (Recursion) of %d and %d is = %d", a, b, gcd_rec(a, b)); 30 printf("\nGCD (Iteration) of %d and %d is = %d", a, b, gcd_ite(a, b)); 31 return 0; 32 } 33 34 int gcd_tail_rec(int a, int b) 35 { 36 if (b == 0) 37 { 38 return a; 39 } 40 else 41 { 42 return gcd_tail_rec(b, a % b); 43 } 44 } 45 46 int gcd_rec(int a, int b) 47 { 48 if (b == 0) 49 { 50 return a; 51 } 52 else 53 { 54 return gcd_rec(b, a % b); 55 } 56 } 57 58 int gcd_ite(int a, int b) 59 { 60 int temp; 61 while (b > 0) 62 { 63 temp = b; 64 b = a % b; 65 a = temp; 66 } 67 return a; 68 }