C program to find GCD of Two Numbers using Recursion

C program to find GCD of Two Numbers using Recursion

Write a program to find GCD of Two Numbers using Recursion

// C program to find GCD of Two Numbers using Recursion

#include <stdio.h>

int gcd(int n1, int n2)
{
	while(n1 != n2)
	{
		if(n1 > n2)
		{
			n1 = gcd(n1 - n2, n2);
		}
		else
		{
			n2 = gcd(n1, n2 - n1);
		}
	}
	return n1;
}

int main()
{
	int n1,n2;
	printf("Enter Number 1 :--> ");
	scanf("%d",&n1);

	printf("Enter Number 2 :--> ");
	scanf("%d",&n2);

	int result = gcd(n1, n2);
	printf("GCD of %d and %d is %d", n1, n2, result);

	return 0;
}

Output:

Enter Number 1 :--> 56
Enter Number 2 :--> 42

GCD of 56 and 42 is 14