The Program in C to calculate the Greatest common divisor of two numbers using non- recursive function is given below:
#include<stdio.h>
int gcd(int a, int b) {
int temp;
while(b != 0) {
temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int num1, num2, result;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
result = gcd(num1, num2);
printf("GCD of %d and %d is: %d", num1, num2, result);
return 0;
}
Output:
Enter two numbers: 45
50
GCD of 45 and 50 is: 5
Pro-Tips💡
This program uses a non-recursive function called “gcd” to find the greatest common divisor of two numbers.
The function uses a while loop to repeatedly apply the Euclidean algorithm until the remainder is 0. The final value of “a” is the GCD.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.