How to write a c program to find GCD of two numbers using For Loop, While Loop, Functions, and Recursion.

## GCD of Two Numbers in C

According to Mathematics, the Greatest Common Divisor (GCD) of two or more integers is the largest positive integer that divides the given integer values without the remainder.

For example, the GCD of two numbers in C, i.e., integer 8 and 12 is 4 because, both 8 and 12 are divisible by 1, 2, and 4 (the remainder is 0) and the largest positive integer among the factors 1, 2, and 4 is 4.

The Greatest Common Divisor (GCD) is also known as the Highest Common Factor (HCF), or Greatest Common Factor (GCF), or Highest Common Divisor (HCD), or Greatest Common Measure (GCM).

## C Program to find GCD of Two Numbers using For Loop

This gcd of two numbers in the C program allows the user to enter two positive integer values. Then, we are going to calculate the Highest Common Factor of those two values. Next, using the For loop, it will calculate the GCD in C.

**NOTE: **To find the Greatest Common Divisor or GCD in C, we have to pass at least one non-zero value.

//C program to find HCF of two numbers #include <stdio.h> int main() { int Num1, Num2, i, GCD; printf("Please Enter two integer Values \n"); scanf("%d %d", &Num1, &Num2); for(i = 1; i <= Num1 && i <= Num2; i++) { if(Num1 % i == 0 && Num2 % i == 0) GCD = i; } printf("GCD of %d and %d is = %d", Num1, Num2, GCD); return 0; }

**OUTPUT**

**ANALYSIS**

Within this gcd program in c example,

- The first C programming printf statement will ask the user to enter two positive integer values.
- For loop will resist the i value, not to exceed the user-specified values.
- In the next line, we used the If Statement to check whether the remainder of Num1 % i and Num2 % i equal to zero or not.

## C Program to find GCD of Two Numbers using While Loop

This GCD of two numbers program in c allows the user to enter two positive integer values. Next, we are going to calculate the Greatest Common Measure of those two values using the While Loop.

//C program to find HCF of two numbers #include <stdio.h> int main() { int Num1, Num2, Temp, GCD; printf("Please Enter two integer Values \n"); scanf("%d %d", &Num1, &Num2); while (Num2 != 0) { Temp = Num2; Num2 = Num1 % Num2; Num1 = Temp; } GCD = Num1; printf("GCD = %d", GCD); return 0; }

**OUTPUT**

**ANALYSIS**

In this gcd program in c program, within the while loop, we are checking whether the second value is not equal to zero or not. If it is zero, it will exit the while loop. Otherwise, it will start executing the statements inside the while loop.

## C Program to find GCD of Two Numbers without using Temp

This gcd program in c allows you to enter two positive integer values. Next, we are going to calculate the Greatest Common Measure of those two values without using the Temporary variable.

//C program to find HCF of two numbers #include <stdio.h> int main() { int Num1, Num2, GCD; printf("Please Enter two integer Values \n"); scanf("%d %d", &Num1, &Num2); if (Num1 == 0) { printf("GCD = %d", Num2); } while (Num2 != 0) { if (Num1 > Num2) { Num1 = Num1 - Num2; } else { Num2 = Num2 - Num1; } } GCD = Num1; printf("GCD = %d", GCD); return 0; }

**OUTPUT**

## C Program to find GCD of Two Numbers Using Functions

This program for gcd of two numbers in c allows the user to enter two positive integer values. Next, we are going to calculate the Greatest Common Divisor of those two values using Functions.

//C program to find HCF of two numbers #include <stdio.h> long gcd(long x, long y); int main() { int Num1, Num2; printf("Please Enter two integer Values \n"); scanf("%d %d", &Num1, &Num2); printf("GCD of %d and %d is = %d", Num1, Num2, gcd(Num1, Num2)); return 0; } long gcd(long x, long y) { if (x == 0) { return y; } while (y != 0) { if (x > y) { x = x - y; } else { y = y - x; } } return x; }

**OUTPUT**

**NOTE: **If we create a function with Void, then there is no need to return any value. However, if we declared a function with any data type (int, float, etc.), then we have to return something out from the function.

## C Program to find GCD of Two Numbers using Recursion

This gcd of two numbers in the program allows the user to enter two positive integer values. Next, we are going to calculate the Greatest Common Divisor of those two values by calling the function recursively. Please refer to Recursion for further reference.

//C program to find HCF of two numbers #include <stdio.h> long gcd(long x, long y); int main() { int Num1, Num2; printf("Please Enter two integer Values \n"); scanf("%d %d", &Num1, &Num2); printf("GCD of %d and %d is = %d", Num1, Num2, gcd(Num1, Num2)); return 0; } long gcd(long x, long y) { if (y == 0) { return x; } else { return gcd(y, x % y); } }

**OUTPUT**

**ANALYSIS**

Within this gcd of two numbers program in c example, when the compiler reaches to *long gcd(long x, long y)* line in the main() program, the compiler will immediately jump to below function:

*long gcd(long x, long y) {*

In the above-specified function, *return gcd(y, x % y)* statement will help the program to call the function Recursively with the updated value. If you miss this statement in gcd of two numbers program, after completing the first line, it will terminate.

Let’s see the If condition in gcd of two numbers program in c. Here, if (y == 0) will check whether the number is equal to 0 or not. For Recursive functions, it is essential to place a condition before using the function recursively. Otherwise, we will end up in infinite execution (Same like Infinite Loop).

Please be careful :)