Calculate the gcd between two natural numbers using the EUCLIDE algorithm.
Example: gcd(1220,516)=?
1220 mod 516 = 188
516 mod 188 = 140
188 mod 140 = 48
140 mod 48 = 44
48 mod 44 = 4
44 mod 4= 0
gcd(1220,516)= 4
Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a%b);
}
void main()
{
int a, b;
printf("Enter 2 integers: ");
scanf("%d %d", &a, &b);
printf("gcd(%d,%d)=%d", a,b,gcd(a,b));
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Using the double hashtag operator in macro expansion