/* program to find the gcd and lcm */
#include<stdio.h>
#include<math.h>
main()
{
int a[10],n,i,j,c,max,min,more;
unsigned long prod;
printf("enter the no of entries: ");
scanf("%d",&n);
printf("enter the entries: ");
for(i=0;i<n;i++)
{
scanf("%d",&c);
if(c>0)
a[i]=c;
else
{
printf("invalid number ");
return;
}
}
max=a[0];
for(i=0;i<n;i++)
if(a[i]>=more)
max=a[i];
min=a[0];
for(i=0;i<n;i++)
if(a[i]<min)
min=a[i];
for(i=0,prod=1;i<n;i++)
prod=prod*a[i];
for(i=max;i<=prod;i+=max)
{
c=0;
for(j=0;j<n;j++)
if(i%a[j]==0)
c+=1;
if(c==n)
{
printf("\nthe lcm of the numbers= %d",i);
break;
}
}
for(i=min;i>0;i--)
{
if (min %i==0)
{
c=0;
for(j=0;j<n;j++)
if(a[j] %i ==0)
c+=1;
}
if(c==n)
{
printf("\nthe gcd of the numbers=%d",i);
break;
}
}
}
No comments:
Post a Comment