# Find GCD of Two Numbers C Program

## C Program To Find GCD of Two Numbers

Learn How To Find GCD of Two Numbers in C Programming Language. GCD is an abbreviated form for **Greatest Common Divisor**. This C Program To Calculate GCD of Two Integers have been developed using a While Loop. The Output of the Code is displayed below.

#### What is GCD?

The **G****reatest Common Divisor** (**GCD**) of Two or more than Two Integers is the Largest Integer (which is Positive) that Divides the Numbers without a Remainder.

#### Example of GCD

The **LCM** of **3** and **2** is **1.**

The **GCD** of **12** and **8** is **4**.

#### Formula To Find GCD of Two Integers

**54**

The Number 54 can be divided by **1, 54, 2, 18, 27, 6, 9, 3**

**24**

The Number 24 can be divided by **1, 24, 3, 8, 12, 2, 4, 6**

**Common Divisors: 1, 2, 3, 6**

**The Largest Divisor is 6**. Therefore, GCD of Integers 54 and 24 is 6.

**Must Read: C Program To Print Strong Numbers From 1 To N**

#### Method 1: C Program To Find GCD of Two Numbers using While Loop

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <stdio.h> int main() { int a, b, num1, num2, temp, gcd; printf("Enter First Integer:\t"); scanf("%d", &num1); printf("\nEnter Second Integer:\t"); scanf("%d", &num2); a = num1; b = num2; while (b != 0) { temp = b; b = a%b; a = temp; } gcd = a; printf("\nGreatest Common Divisor of %d and %d = %d\n", num1, num2, gcd); printf("\n"); return 0; } |

**Must Read: Find LCM of Two Integers in C Programming**

#### Method 2: C Program To Find GCD of Two Integers using For Loop

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include<stdio.h> int main() { int a, b, num1, num2, temp, gcd; printf("Enter First Integer:\t"); scanf("%d", &num1); printf("\nEnter Second Integer:\t"); scanf("%d", &num2); for(a = num1, b = num2; b != 0;) { temp = b; b = a%b; a = temp; } gcd = a; printf("\nGreatest Common Divisor of %d and %d = %d\n", num1, num2, gcd); printf("\n"); return 0; } |

**Must Read: C Program To Convert Integers into Words (Strings)**

#### Output

In case you find any error in the above C Program To Find GCD of Two Numbers or if you have any doubts, let us know about it in the Comment Section below.

Is this code compatible to work in Macintosh OS?

Yes. I don’t think there would be any problem in compiling this program in your Macbook.

This is really so simple to understand. Thank you so much.

Is it possible to solve gcd of 2 integers using recursion function?

After getting gcd of integers, it is very easy to get lcm. Excellent code. Simplifies the logic.