Monday, 26 December 2011

C program to find the lcm and hcf of given numbers using Euclid's Algorithm



#include<stdio.h>
int hcff(int,int);
int lcmf(int,int);
main()
{
int i,a[100],n,lcm,hcf;
printf("Enter the limit\n");
scanf("%d",&n);
printf("Enter the numbers\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
hcf=a[0];
for(i=1;i<n;i++)
hcf=hcff(hcf,a[i]);
printf("HCF = %d\n",hcf);
lcm=a[0];
for(i=1;i<n;i++)
lcm=lcmf(lcm,a[i]);
printf("LCM = %d",lcm);
}

int hcff(int m,int n)
{
int r,t;
if(m<n)
{
t=m;
m=n;
n=t;
}
while(1)
{
r=m%n;
if(r==0)
return n;
else
m=n;
n=r;
}
}

int lcmf(int m,int n)
{
int lcm;
lcm=m*n/hcff(m,n);
return lcm;
}



You Like It!? Then kindly share with your Friends.
Comments
1 Comments

1 comment:

Post a Comment