bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / practice-c / pc-ip-15.c

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 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror