Home >>c programs >GCD Program in C

GCD Program in C

C Program to find GCD of two Numbers

GCD program in C is basically used to find the largest integer that can exactly divide both the provided numbers. GCD basically elaborates to Greatest Common Divisor.

The GCD is also known as HCF that elaborates to Highest Common Factor. The GCD or HCF of two integers num1 and num2 is basically the largest integer that can exactly divide both n1 and n2 and that simply means that it should not leave a remainder.

Finding GDC in C is extremely easy as it has so many operators to work with and the C programming is easy for the most of the programmers.

Here is a C program to find GCD of two numbers is depicted below that will give you a brief understanding of the topic:

#include <stdio.h>
int main()
{
    int num1, num2, i, gcd;
    printf("Please Enter Your two integer number ");
    scanf("%d %d", &num1, &num2);
    for(i=1; i <= num1 && i <= num2; ++i)
    {
        //First Check whether i is factor of both integers
        if(num1%i==0 && num2%i==0)
		gcd = i;
    }
    printf("Here is the GCD of  %d and %d is %d", num1, num2, gcd);
    return 0;
}
Output :
Please Enter Your two integer number 10 15
Here is the GCD of 10 and 15 is 5