Friday 26 September 2014

LCM of n numbers

Write a C program that calculates the least common multiple (LCM) of 'n' numbers.

Input Format:
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

Output Format:
One line containing the LCM of the 'n' numbers.




#include<stdio.h>

int main()
{int i,n,j,l=0;int k=0;
scanf("%d",&n);
int a[10000];
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);

}
for(i=1;l<2;i++)
{k=0;
for(j=1;j<=n;j++)
{
if(i%a[j]==0)
k++;
}
if(k==n)
{printf("%d",i);l=3;}
}
return 0;}

4 comments:

  1. Replies
    1. if l=3,than loop will be over,
      as the condition of running the loop is l<2.

      Delete
  2. we can use break instead of l=3?

    ReplyDelete
  3. I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! lcm of two numbers in c

    ReplyDelete