8️⃣C Program to Find GCD of Two Numbers

 

✅ C Program to Find GCD of Two Numbers

๐Ÿ“Œ What is GCD?

GCD (Greatest Common Divisor) is the largest positive number that divides two numbers without leaving a remainder.

๐Ÿ”น Example:

  • Numbers: 24 and 36

  • Common divisors: 1, 2, 3, 4, 6, 12

  • Greatest among them = 12

๐Ÿ‘‰ So, GCD of 24 and 36 is 12


๐Ÿง  Method Used: Euclidean Algorithm

The Euclidean Algorithm is the most efficient way to find GCD.

Rule:

GCD(a, b) = GCD(b, a % b)

Repeat until b becomes 0.
The remaining value of a is the GCD.


๐Ÿ’ป C Program to Find GCD of Two Numbers

#include <stdio.h> int main() { int a, b, temp; printf("Enter two numbers: "); scanf("%d %d", &a, &b); while (b != 0) { temp = b; b = a % b; a = temp; } printf("GCD is: %d", a); return 0; }

๐Ÿงช Example Output

Input:

Enter two numbers: 24 36

Output:

GCD is: 12

๐Ÿ” Step-by-Step Explanation

Let input be:

a = 24, b = 36
Stepaba % b
1243624
2362412
324120

When b = 0,
a = 12 → This is the GCD


๐Ÿงฉ Explanation of Code

  • scanf() → Takes two numbers from user

  • while (b != 0) → Loop runs until remainder becomes 0

  • a % b → Finds remainder

  • temp → Stores value to swap numbers

  • Final value of aGCD   

✅ Conclusion

In this program, we learned how to calculate the GCD of a Number in C.


๐Ÿ“˜ C Programming Series – What’s Next?

๐Ÿ‘‰ In the next post of this series, we will learn how to Find LCM of Two Numbers with complete explanation and output examples. Stay tuned! ๐Ÿš€

Comments

Popular posts from this blog

⓮C Program to calculate Simple Interest (With Program and Explanation)

⑫ C Program to Swap Two Numbers Without Using a Temporary Variable (With Program and Explanation)

9️⃣C Program to Find LCM of Two Numbers (With Program and Explanation)