Write a program to find GCD(Greatest Common Divisor) or HCF(Highest Common Factor)of two numbers using recursion. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Write a program to find GCD(Greatest Common Divisor) or HCF(Highest Common Factor)of two numbers using recursion.

GCD Finder using recursion and ternary operator https://code.sololearn.com/cW2QaKk3aDM8/?ref=app

28th May 2017, 12:45 PM
SUNNY KUMAR
SUNNY KUMAR - avatar
4 Answers
+ 3
find gcd with int gcd(int a, int b) { return (a % b == 0) ? b : gcd(b, a%b); } *Must set 'a' larger than 'b'
29th May 2017, 4:10 AM
OrbitHv [Inactive]
OrbitHv [Inactive] - avatar
+ 1
Oh.. Wasn't it a question? I'm sorry I didn't see your code well..
28th May 2017, 5:21 PM
OrbitHv [Inactive]
OrbitHv [Inactive] - avatar
+ 1
it doesn't matter either a is greater or b
28th May 2017, 5:47 PM
SUNNY KUMAR
SUNNY KUMAR - avatar
0
To print the Greatest Common Divisor (GCD) of two numbers using recursion, you can utilize the Euclidean algorithm. #include <stdio.h> // Function to find GCD of a and b int gcd(int a, int b) { if (b == 0) // Base case return a; else return gcd(b, a % b); // Recursive call } int main() { int num1, num2; printf("Enter two positive integers: "); scanf("%d %d", &num1, &num2); printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2)); return 0; } For more detailed explanations refer to the post on [GCD using Recursion in C Program-TeachingBee][1]. [1]: https://teachingbee.in/blog/gcd-using-recursion-in-c-program/
2nd Mar 2024, 10:28 AM
WorkAnything Pvt. Limited
WorkAnything Pvt. Limited - avatar