This C program is to find lcm and gcd/hcf of two numbers using function.For example, lcm and gcd/hcf of two numbers using function 12 and 8(lcm=24 gcd=4). Tags for GCD - Greatest Common Divisor using Functions in C. Sample hcf; sample gcd; greatest common divisor program using fucntions; find gcd in c; finding hcf; highest common factor; c program to find gcd of two numbers; c; c program for gcd using function; c program to find gcd of three numbers using function; c program to find gcd using.
- 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.
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
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.