Home / Programs / Program to find GCD of two numbers using the recursive and iterative method
Programming Example

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

👁 1,099 Views
💻 Practical Program
📘 Step by Step Learning
Program to find GCD of two numbers using the recursive and iterative method

Program Code

#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

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.