Ticker

6/recent/ticker-posts

Header Ads Widget

Responsive Advertisement

GCD of Two Numbers using C

Problem Statement


In this program we will code a basic program to calculate GCD of two numbers in C Programming language.

Greatest Common Divisor or GCD of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. For example, the gcd of 8 and 12 is 4.

#include 

int gcd(int a, int b)

{

    if (b == 0)

        return a;

    return gcd(b, a % b); 

}

int main()

{

    int n1,n2;

    printf("Enter two numbers : ");

    scanf("%d%d",&n1,&n2);

    printf("GCD of %d and %d is %d.", n1, n2, gcd(n1, n2));

    return 0;

}

Output

Enter two numbers : 10 100

GCD of 10 and 100 is 10.

Post a Comment

0 Comments