HCF and LCM

//HCF and LCM
#include<stdio.h>
void main()
{
int n1,n2,a,b,LCM,HCF;
printf(“enter the value of two numbers:”);
scanf(“%d%d”,&n1,&n2);
a=n1;
b=n2;
while(n1!=n2)
{
if(n1>n2)
{
n1=n1-n2;
n2=n2;
}
else
{
n1=n1;
n2=n2-n1;
}
}                                                    //coment Section
HCF=n1;                                     // At this point both the value of n1 and n2 so                                                           writting HCF=n2 is same thing.
LCM=(a*b)/HCF;                  // LCM= number1*number2/HCF. Here we need                                                    // initial value of n1 and n2 which is equal to a and                                                   //    b.
printf(” LCM=%d\n HCM=%d”,LCM,HCF);

}

OUTPUT

There are multiple logic to find hcf and lcm. This is the easiest i feel. If you find any confusion with the logic, you can comment on it.

NOTE:- If you are using Turbo C compiler then you must have to include conio.h and insert  getch(); to see the result.

Leave a Reply

Your email address will not be published.