Home

A program to find out GCD of two numbers using RECURSION

C Programming Language 087

Program

#include <stdio.h>

void main()
{
    int n1, n2, gcd;
    printf("Enter two numbers: ");
    scanf("%d %d", &n1, &n2);

    gcd = fgcd(n1, n2);

    printf("GCD of %d & %d is: %d", n1, n2, gcd);
    printf("\n");
}

int fgcd(int x, int y)
{
    while (x != y)
    {
        if (x > y)
        {
            return fgcd(x - y, y);
        }
        else
        {
            return fgcd(x, y - x);
        }
    }
    return x;
}

Result

Enter two numbers: 56 49
GCD of 56 & 49 is: 7


Last Updated on

Next Post: GraphProtocol: TS2322 null assignment in Subgraph →

Comments