C Program to find GCD of two numbers using for loop. Kindly visit the site to check out the program to find the Greatest Common Divisor ( GCD ) of given any two. How can I write a program to find the GCD between two numbers in C? In an array, excluding the largest prime in that array using functions in C program?
- Related Questions & Answers
- Selected Reading
The Greatest Common Divisor (GCD) of two numbers is the largest number that divides both of them.
For example: Let’s say we have following two numbers: 45 and 27
A program to find the GCD of two numbers using recursion is given as follows.
Example
Output
In the above program, gcd() is a recursive function. It has two parameters i.e. a and b. If a or b is 0, the function returns 0. If a or b are equal, the function returns a. If a is greater than b, the function recursively calls itself with the values a-b and b. If b is greater than a, the function recursively calls itself with the values a and b-a.
![C program to find gcd of two numbers using recursion C program to find gcd of two numbers using recursion](https://www.tutorialgateway.org/wp-content/uploads/C-Program-to-find-GCD-of-Two-Numbers-1.png)
This is demonstrated by the following code snippet.
Another method of finding the GCD of two numbers using recursion is as follows.
Example
Output
In the above program, gcd() is a recursive function. It has two parameters i.e. a and b. If b is greater than 0, then a is returned to the main() function. Otherwise, the gcd() function recursively calls itself with the values b and a%b.
This is demonstrated using the following code snippet.
- Related Questions & Answers
- Selected Reading
The Greatest Common Divisor (GCD) of two numbers is the largest number that divides both of them.
For example: Let’s say we have two numbers are 45 and 27.
So, the GCD of 45 and 27 is 9.
A program to find the GCD of two numbers is given as follows.
Example
Output
In the above program, gcd() is a recursive function. It has two parameters i.e. a and b. If b is greater than 0, then a is returned to the main() function. Otherwise the gcd() function recursively calls itself with the values b and a%b. This is demonstrated by the following code snippet −
Another program to find the GCD of two numbers is as follows −
Example
![How to find gcd How to find gcd](https://d5ngkkf53wl41.cloudfront.net/tutorials/c-programming/c-program-to-find-gcd-using-for-loop-and-if-statement.gif)
Output
In the above program, gcd() is a recursive function. It has two parameters i.e. a and b. If a or b is 0, the function returns 0. If a or b are equal, the function returns a. If a is greater than b, the function recursively calls itself with the values a-b and b. If b is greater than a, the function recursively calls itself with the values a and (b - a). This is demonstrated by the following code snippet.