C Program to Find HCF (GCD) and LCM of Two Numbers


This C program calculates Highest Common Factor (HCF) & Least Common Multiple (LCM) of two numbers given by user. HCF is also known as Greatest Common Divisor (GCD).

Highest Common Factor (HCF): The greatest common factor to any two or more than two integer numbers is known as HCF of these numbers. For example, HCF of 12 and 18 is 6. Also try: Calculate HCF Online

Lowest Common Multiple (LCM): The smallest or lowest common multiple of any two or more than two integer numbers is termed as LCM. For example, LCM of 12 and 18 is 36. Also try: Calculate LCM online

C Source Code: HCF & LCM


#include<stdio.h>
#include<conio.h>

int main()
{
	 int num1, num2, hcf, lcm, i;
	 clrscr();
	 printf("Enter first number: ");
	 scanf("%d", &num1);
	 printf("Enter second number: ");
	 scanf("%d", &num2);
	 /* Finding HCF */
	 /* You can check i<=num2 in condition of for loop. */
	 for(i=1; i<=num1; i++)
	 {
		  if(num1%i==0 && num2%i==0)
		  {
		   	hcf = i;
		  }
	 }
	 /* Finding LCM */
	 lcm = (num1 * num2)/hcf;
	 printf("HCF = %d and LCM = %d", hcf, lcm);
	 getch();
	 return(0);
}

HCF & LCM C Program Output

Enter first number: 24 ↲
Enter second number: 32 ↲
HCF = 8 and LCM = 96 

Note: ↲ indicates enter is pressed.