C program to find the GCD and LCM of two integers using Euclids’ algorithm

/*
* C program to find the GCD and LCM of two integers using Euclids' algorithm
*/
#include <stdio.h>
 
void main()
{
	int num1, num2, gcd, lcm, remainder, numerator, denominator;
 
	printf("Enter two numbers\n");
	scanf("%d %d", &num1, &num2);
	if (num1 > num2)
	{
		numerator = num1;
		denominator = num2;
	}
	else
	{
		numerator = num2;
		denominator = num1;
	}
	remainder = num1 % num2;
	while (remainder != 0)
	{
		numerator = denominator;
		denominator = remainder;
		remainder = numerator % denominator;
	}
	gcd = denominator;
	lcm = num1 * num2 / gcd;
	printf("GCD of %d and %d = %d\n", num1, num2, gcd);
	printf("LCM of %d and %d = %d\n", num1, num2, lcm);
}

Capture
Output
 
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s