26 December 2011

C Program To Find LCM and HCF of a Number

1 comment

Program to find LCM and HCF of a Number







#include<stdio.h>

//declaration of lcm function
int lcm(int,int);

//declaration of hcf function
int hcf(int,int);

void main()
{
int d,a,b;
printf("enter the two number whose lcm or hcf should be find :" );
scanf("%d%d",&a,&b);
printf( "the hcf of the numbers is:");
d = hcf(a,b);
printf("%d",d);
printf( "\nthe lcm of the numbers is:");
d = lcm(a,b);
printf("%d",d);
}

//definition of lcm function
int lcm(int x, int y)
{
int b;
b = x*y/hcf(x,y);
return b;
}

//definition of hcf function
int hcf(int x,int y)
{
int temp, d;
if(y>x)
{
temp = x;
x=y;
y = temp;
}
d = y;
while(1)
if(x%d==0&&y%d==0)
{
return d;
}
else
d --;
}

If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

1 comment:

  1. It would be kind of handy for school...along with "Qalculate!", but I only know how to do some basic scripts in Fedora due to bits and pieces...nice!

    ReplyDelete