Showing posts with label GCD. Show all posts
Showing posts with label GCD. Show all posts

Sunday, 31 July 2016

GCD of two numbers using Recursion

Recursion: Greatest Common Divisor using Euclid's Algorithm


Write a program to find a GCD of two numbers using Recursion.

#include<stdio.h>
int gcd(int a, int b);

void main()
{
                int a, b, g;
                printf("\nEnter the value of a and b:");
                scanf("%d%d", &a, &b);
                g = gcd(a, b) ;
                printf("\n GCD = %d", g);
}
int gcd(int a, int b)
{
                if(b == 0)
                                return a;
                else
                                return gcd(b, a%b);
}

Output:
Enter the value of a and b: 4 6

GCD = 2