Write a program to find GCD of two number

Write a program to find GCD of two number.
using function

  1. #include<stdio.h>
  2. int find_gcd(int n1, int n2);

  3. int main() {
  4.     int n1, n2;
  5.     printf("Enter two numbers to find GCD : ");
  6.     scanf("%d %d", & n1, & n2);
  7.     
  8.     printf("\n%d and %d of GCD is: %d\n\n", n1, n2, find_gcd(n1, n2));
  9.  
  10.     return 0;
  11.  }
  12. int find_gcd(int n1, int n2) {
  13.   if (n2 == 0)
  14.     return n1;
  15.   else
  16.     return find_gcd(n2, n1 % n2);
  17. }

Using recursion 

  1. #include<stdio.h>

  2. int find_gcd(int , int );

  3. int main()
  4. {
  5.     int n1, n2, gcd;
  6.     printf("Enter two numbers to find GCD : ");
  7.     scanf("%d%d", &n1, &n2);
  8.     
  9.     gcd = find_gcd(n1, n2);
  10.     printf("\n%d and %d of GCD is: %d\n\n", n1, n2, gcd);

  11.     return 0;
  12. }

  13. int find_gcd(int x, int y)
  14. {
  15.     if(x > y)
  16.         find_gcd(x-y, y);

  17.     else if(y > x)
  18.         find_gcd(x, y-x);
  19.     else
  20.         return x;
  21. }

একটি মন্তব্য পোস্ট করুন