GCM & LCM calculation


Using this program you can find the GCM&LCM of n numners.....
#include<stdio.h>
#include<math.h>
#include<conio.h>

void main()
{
int gcm,lcm=1,i,num[100],n,min,flag=1;

printf("enter how many numbers you want = ");
scanf("%d",&n);

printf("\nenter your numbers : \n");
for(i=0;i<n;i++)
{
printf("%d =",i);
scanf("%d",&num[i]);
}

min=num[0];
for(i=1;i<n;i++)
{
if(min>num[i])
{
min=num[i];
}
}

for(;flag==1;)
{
for(i=0;i<n;i++)
{
if(num[i]%min==0)
{
flag=2;
}
else
{
min--;
flag=1;
break;
}
}
}

printf("gcm =%d",min);


for(i=0;i<n;i++)
{
lcm=lcm*(num[i]/min);
}
lcm=lcm*min;

printf("\nlcm = %d",lcm);
getch();
return;
}

Post a Comment

0 Comments