Easy method to find LCM of given number of elements using C
LCM of given number of elements :
We will use a method -->
1.Find the minimum element
2.Find a number that is divisible that can be divided by ell these numbers.
3.Hence are left with the number that is your required LCM.
_____________________________________________________________
#include<stdio.h>
int findmin(int n,int ar[100])
{
int i=0,min;
min = ar[0];
for(i=0;i<n;i++)
{
if(ar[i]<min)
{
min = ar[i];
}
}
return min;
}
int modcheck(int n, int arr[100],int minia)
{
int bolia = 0;
for(int i=0;i<n;i++)
{
if(minia % arr[i]!= 0)
{
return 0;
}
}
return 1;
}
int main()
{
int n,n1,n2,minMul;
int a[100];
printf("enter the number of numbers :");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
minMul = findmin(n,a);
printf("%d",minMul);
while(1)
{
if(modcheck(n,a,minMul)!=0)
{
printf("The LCM is %d",minMul);
break;
}
++minMul;
}
return 0;
}
We will use a method -->
1.Find the minimum element
2.Find a number that is divisible that can be divided by ell these numbers.
3.Hence are left with the number that is your required LCM.
_____________________________________________________________
#include<stdio.h>
int findmin(int n,int ar[100])
{
int i=0,min;
min = ar[0];
for(i=0;i<n;i++)
{
if(ar[i]<min)
{
min = ar[i];
}
}
return min;
}
int modcheck(int n, int arr[100],int minia)
{
int bolia = 0;
for(int i=0;i<n;i++)
{
if(minia % arr[i]!= 0)
{
return 0;
}
}
return 1;
}
int main()
{
int n,n1,n2,minMul;
int a[100];
printf("enter the number of numbers :");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
minMul = findmin(n,a);
printf("%d",minMul);
while(1)
{
if(modcheck(n,a,minMul)!=0)
{
printf("The LCM is %d",minMul);
break;
}
++minMul;
}
return 0;
}
Comments
Post a Comment