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:
Repeat until b becomes 0.
The remaining value of a is the GCD.
๐ป C Program to Find GCD of Two Numbers
๐งช Example Output
Input:
Output:
๐ Step-by-Step Explanation
Let input be:
| Step | a | b | a % b |
|---|---|---|---|
| 1 | 24 | 36 | 24 |
| 2 | 36 | 24 | 12 |
| 3 | 24 | 12 | 0 |
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
a→ GCD
Comments
Post a Comment