C Program to find HCF and LCM
GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them.
For example GCD of 20 and 28 is 4 and GCD of 98 and 56 is 14.
LCM (Least Common Multiple) of two numbers is the smallest number which can be divided by both numbers.
For example, LCM of 15 and 20 is 60, and LCM of 5 and 7 is 35.
Program to find GCD & LCM:
#include <stdio.h>
int main()
{
int i, num1, num2, min, hcf=1,lcm;
/* Input two numbers from user */
printf("Enter any two numbers to find HCF & LCM: ");
scanf("%d%d", &num1, &num2);
/* Find minimum between two numbers */
min = (num1<num2) ? num1 : num2;
for(i=1; i<=min; i++)
{
/* If i is factor of both number */
if(num1%i==0 && num2%i==0)
{
hcf = i;
}
}
printf("HCF of %d and %d = %d\n", num1, num2, hcf);
lcm = ((num1*num2)/hcf);
printf("LCM of %d and %d = %d\n", num1, num2, lcm);
return 0;
}
Output:
Enter any two numbers to find HCF & LCM: 15 20
HCF of 15 and 20 = 5
LCM of 15 and 20 = 60
Enter any two numbers to find HCF & LCM: 20 28
HCF of 20 and 28 = 4
LCM of 20 and 28 = 140
Run Code- If you want run this code copy this code, paste here and run.
No comments: