Write a program which reads two positive integer values and shows their GCD (Greatest Common Divisor) and their LCM (Least Common Multiple).


Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
int 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);
	printf("LCM(%d,%d)=%d\n", A, B, A*B/X);

	return 0;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Complexity of sum of square root