C program to find LCM & GCD of two numbers

C program to find LCM & GCD of two numbers

This C Program calculates the GCD and LCM of two integers.

Here GCD means Greatest Common Divisor. For two integers a and b, if there are any numbers d so that a / d and b / d doesn’t have any remainder, such a number is called a common divisor. Common divisors exist for any pair of integers a and b since we know that 1 always divides any integer. We also know that the common divisor can’t get too big since divisors can’t be any larger than the number they are dividing. Hence a common divisor d of a and b must have d ≤ a and d ≤ b.

Here, LCM means Least Common Multiplies. For two integer a & b, to know if there are any smallest numbers d so that d / a and d / b doesn’t have a remainder. such a number is called the Least Common Multiplier.


/*
	Program to find LCM of two numbers without recursion
	Author 			: 		Krishna Teja G S
	Repository		:		github.com/packetprep/coding-questions
	Website			: 		packetprep.com
*/

#include

int main(){

	int  a,b,multiple,lcm;

	printf("Enter the two numbers: ");
	scanf("%d %d",&a,&b);

	multiple = (a < b) ? a : b;
	// LCM logic
	while(1){
		if(multiple % a == 0 && multiple % b == 0){
			lcm = multiple;
			break;
		}
		multiple++;
	}

	printf("The LCM of %d and %d is %d \n",a,b,lcm);

}



/*
	Program to find GCD of two numbers without recursion
	Author 			: 		Krishna Teja G S
	Repository		:		github.com/packetprep/coding-questions
	Website			: 		packetprep.com
*/

#include

int main(){

	int  a,b,i,gcd;

	printf("Enter the two numbers: ");
	scanf("%d %d",&a,&b);

	// GCD logic
	for(i=1;i<=a && i<=b;i++){
		if( a % i == 0 &&  b % i == 0){
			gcd = i;
		}
	}

	printf("The GCD of %d and %d is %d \n",a,b,gcd);

}
wipro free mock test
Wipro National Qualifier Test is just a few days away. To help you crack the exam we are sharing a mock test specially designed for Wipro based on all the previous papers.