C Program to Find GCD of Two Numbers

CodingTute

GCD of Two Numbers

In this example, you will learn to find the GCD or HCF of two given numbers.

GCD is known as Greatest Common Divisor, also called HCF, which means Highest Common Factor. It is the highest common number that perfectly divides the given numbers leaving the remainder as zero.

C Program to find GCD or HCF of Two Numbers

// C program to find GCD/HCF of two numbers
#include <stdio.h>

// Recursive function to return GCD of num1 and num2
int gcd(int num1, int num2)
{
	if (num2 == 0)
		return num1;
	return gcd(num2, num1 % num2);
}

int main()
{
	int num1, num2;
	printf("Enter the two numbers: ");
	scanf("%d %d",&num1,&num2);
	
	printf("GCD of %d and %d is %d.",num1,num2,gcd(num1,num2));
}

Output

Enter the two numbers: 10 15
GCD of 10 and 15 is 5.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.