Find GCD of Two Numbers


/*
Write a program to find GCD of two numbers.
*/


#include<stdio.h>


int GCD(int a, int b);
int gcd(int a, int b);


main()
{
int a, b;
printf("Enter a and b : \n");
scanf("%d%d",&a, &b);
printf("%d\n",GCD(a,b));
printf("%d\n",gcd(a,b));
}


/*Recursive*/
int GCD(int a, int b)
{
if(b==0)
return a;
return GCD(b, a%b);
}

/*Iterative*/
int gcd(int a, int b)
{
int rem;
while(b != 0)
{
rem = a%b;
a = b;
b = rem;
}
return a;
}

Post a Comment

0 Comments