Sunday 30 June 2013

Write a program using a recursive function to find the gcd (greatest common divisor) of two given numbers.

#include<stdio.h>
#include <conio.h>

int gcd ( int a, int b ) 
{
    int r;
    if ( ( r = a%b ) == 0 )
       return b;
    else
       return gcd ( b , r );
}
void main(void){
   int p,q;
   printf("\n Enter two numbers: ");
   scanf("%d %d",&p,&q);
   printf("\n The gcd is  %d ",gcd(p,q));
   getch( );
}

No comments:

Post a Comment