Write a program to calculate the GCD of given numbers using recursion

 How to find GCD:-

                            The Greatest Common Divisor (GCD) for 8, and 12, notation CGD(8,12), is 4.

Explanation:

  •  The factors of 8 are 1,2,4,8;
  •  The factors of 12 are 1,2,3,4,6,12;
  • So greatest common divisor is 4.

Code:-

#include<stdio.h>

// function for calculating the GCD

int GCD(int a,int b,int n)
{
if( a%n==0 && b%n==0)
return n;
else
{
n=n1;
GCD(a,b,n);
}
}
int main()
{
int a,b,n,g;
printf(“Enter two numbern”);
scanf(“%d%d”,&a,&b);
n=a>b?b:a; // for finding greater number
g=GCD(a,b,n); // function calling
printf(“GCD of the numbers is: %d”,g);
return 0;
}


Output:

Enter two number
12 8
GCD of the numbers is: 4



Leave a Reply

Your email address will not be published.