LCM of an array of integers
#include <stdio.h>
#include <stdlib.h>
int mul(int arr[],int n);
int maxArrElement(int arr[],int n);
int main()
{
int L,n,i,j,arr[100],max,multiplication;
printf("Enter the no. of terms: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
max=maxArrElement(arr,n);
multiplication=mul(arr,n);
printf(" The maximum no is : %d\n",max);
printf("Multiplication of Array elements is : %d\n",multiplication);
for(L=max;L<=multiplication;L+=max)
{
for(j=0;j<n;j++)
{
//....You have to put general condition for any no of array elements...
if(L%arr[0]==0 && L%arr[1]==0 && L%arr[2]==0 && L%arr[3]==0 && L%arr[4]==0 )
break;
}
}
printf("The LCM is %d\n",L);
return 0;
}
int mul(int arr[],int n)
{
int i,res=1;
for(i=0;i<n;i++)
{
res*=arr[i];
}
return res;
}
int maxArrElement(int arr[],int n)
{
int max,i;
max=arr[0];
for(i=0;i<n;i++)
{
if(max<arr[i])
max=arr[i];
}
return max;
}
#include <stdlib.h>
int mul(int arr[],int n);
int maxArrElement(int arr[],int n);
int main()
{
int L,n,i,j,arr[100],max,multiplication;
printf("Enter the no. of terms: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
max=maxArrElement(arr,n);
multiplication=mul(arr,n);
printf(" The maximum no is : %d\n",max);
printf("Multiplication of Array elements is : %d\n",multiplication);
for(L=max;L<=multiplication;L+=max)
{
for(j=0;j<n;j++)
{
//....You have to put general condition for any no of array elements...
if(L%arr[0]==0 && L%arr[1]==0 && L%arr[2]==0 && L%arr[3]==0 && L%arr[4]==0 )
break;
}
}
printf("The LCM is %d\n",L);
return 0;
}
int mul(int arr[],int n)
{
int i,res=1;
for(i=0;i<n;i++)
{
res*=arr[i];
}
return res;
}
int maxArrElement(int arr[],int n)
{
int max,i;
max=arr[0];
for(i=0;i<n;i++)
{
if(max<arr[i])
max=arr[i];
}
return max;
}
Comments
Post a Comment