Program to find GCD of two numbers using the recursive and iterative method

C Programming Language / Recursion in c

1012

Program:

#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));
}/*End of main()*/


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


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

Output:

Enter a and b :
12
15
3
3
Press any key to continue . . .

Explanation:

Program to find GCD of two numbers using the recursive and iterative method

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.