+ 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
4 odpowiedzi
+ 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'
+ 1
Oh.. Wasn't it a question? I'm sorry I didn't see your code well..
+ 1
it doesn't matter either a is greater or b
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/