Use of a recursive function to compute the GCD of two positive integers in C

#include <stdio.h>
 
 int gcd(int,int);
 
 int main()
 {
    int a,b;
 
    printf("Enter the values of a and b : ");
    scanf("%d%d",&a,&b);
    printf("nGCD = %dn",gcd(a,b));
    return 0;
 }
 
 int gcd(int p,int q)
 {
    int r;
 
    if((r=p%q)==0)
        return q;
    else
        return gcd(q,r);
 }

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.