First line contains the number of numbers that are input 'n', where n>1
Second line contains 'n' positive integers whose LCM is to be calculated
Program:
#include<stdio.h>
int gcd(int,int); /* function prototype */
int lcm(int,int); /* function prototype */
int main()
{
int n,i,l,num;
l=1;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&num);
l=lcm(num,l); /* recursive function call */
}
printf("%d",l);
return 0;
}
int lcm(int a, int b) /* function to find LCM of two numbers */
{
int l;
l=(a*b)/(gcd(a,b));
return l;
}
int gcd(int a, int b) /* function to find gcd of two numbers */
{
int t;
while(b!=0)
{
if(a<b) /* if a<b then swap */
{
t=a;
a=b;
b=t;
}
t=a;
a=b;
b=t%b;
gcd(a,b);
}
return a;
}
int gcd(int,int); /* function prototype */
int lcm(int,int); /* function prototype */
int main()
{
int n,i,l,num;
l=1;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&num);
l=lcm(num,l); /* recursive function call */
}
printf("%d",l);
return 0;
}
int lcm(int a, int b) /* function to find LCM of two numbers */
{
int l;
l=(a*b)/(gcd(a,b));
return l;
}
int gcd(int a, int b) /* function to find gcd of two numbers */
{
int t;
while(b!=0)
{
if(a<b) /* if a<b then swap */
{
t=a;
a=b;
b=t;
}
t=a;
a=b;
b=t%b;
gcd(a,b);
}
return a;
}
Sample input and output:
8
5 15 45 3 6 4 8 4
360
5 15 45 3 6 4 8 4
360