C Program to Find GCD of given Numbers using Recursion
Posted by Superadmin on December 09 2015 05:14:17
This C program, using recursion, finds the GCD of the two numbers entered by the user. The user enters two numbers by using a space in between them or by pressing enter after each input.
Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to find GCD of given Numbers using Recursion
*/
#include <stdio.h>
int gcd(int, int);
int main()
{
int a, b, result;
printf("Enter the two numbers to find their GCD: ");
scanf("%d%d", &a, &b);
result = gcd(a, b);
printf("The GCD of %d and %d is %d.\n", a, b, result);
}
int gcd(int a, int b)
{
while (a != b)
{
if (a > b)
{
return gcd(a - b, b);
}
else
{
return gcd(a, b - a);
}
}
return a;
}}
$ gcc gcd_recr.c -o gcd_recr
$ a.out
Enter the two numbers to find their GCD: 100 70
The GCD of 100 and 70 is 10.