Tuesday 30 April 2013

H.C.F. AND L.C.M. - SIMPLE C PROGRAM

H.C.F.
The highest common factor (H.C.F.) of two or more given numbers is the greatest number which divides each of given numbers exactly without leaving any remainder.

L.C.M.
The least common multiple (L.C.M.) of two given numbers is that lowest number which is divisible exactly by each of the given numbers.

Note:-
Product of two numbers = product of their H.C.F. and L.C.M.


Source code for finding H.C.F. and L.C.M. through a C program:-
/********************************************************************************/

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

int main()
{
long num1,num2,hcf,lcm,div,r;
clrscr();
printf("ENTER TWO NUMBERS : ");
scanf("%ld%ld",&num1,&num2);

hcf=num1>num2?num2:num1;
div=num1>num2?num1:num2;
r=div%hcf;

while(r)
{
div=hcf;
hcf=r;
r=div%hcf;
}

lcm=(num1*num2)/hcf;
printf("hcf = %ld\t\tlcm = %ld",hcf,lcm);
getch();
return 1;
}
/****************************************************************************/

No comments:

Post a Comment