Sunday, December 12, 2010

Hcf and Lcm Of given numbers

//PROGRAM TO FIND THE HCF AND LCM OF THE GIVEN NUMBERS.

#include
#include
int hcf(int a,int b);
int lcm(int x,int y);
void main()
{
int n,ch,num[10],hc,lc,i;
clrscr();
printf("\nEnter the value of n:"); //reading numbers
scanf("%d",&n);
printf("Enter the %d numbers:",n);
for(i=0;i scanf("%d",&num[i]);

hc=num[0]; //calculating hcf
for(i=1;i hc=hcf(hc,num[i]);
printf("\nThe HCF is %d",hc);

lc=num[0]; //calculating lcm
for(i=1;i lc=lcm(lc,num[i]);
printf("\nThe LCM is %d",lc);

getch();
}

int hcf(int a,int b) //hcf function
{
if(b==0)
return a;
else
return hcf(b,a%b);
}

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

No comments:

Post a Comment