Pages

Tuesday, 16 June 2015

GCD of two numbers using recursion

GCD of two numbers using recursion

Coding:

#include<stdio.h>
int gcd(int a,int b)
{
if(a%b==0)
return b;
else
return gcd(b,a%b);
}
int main()
{
int no1,no2;
printf("Please enter the first number:");
scanf("%d",&no1);
printf("Please enter the second number:");
scanf("%d",&no2);
printf("GCD is:%d",gcd(no1,no2));
return 0;
}

Output:
Please enter the first number:16
Please enter the second number:12
GCD is:4