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
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>
void main()
{
int A, B; /* data */
int X, Y, REMAINDER;
do {
printf("Enter A (different than 0) : ");
scanf("%d", &A);
} while(!A);
do {
printf("Enter B (different than 0) : ");
scanf("%d", &B);
} while(!B);
for (REMAINDER=A, X=A, Y=B ; REMAINDER ; X=Y, Y=REMAINDER)
REMAINDER = X%Y;
printf("GCD(%d,%d)=%d\n", A, B, X);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Decimal to binary