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 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()*/
                        

🖥 Program 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
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.