## C Program To Find LCM of N Numbers

Learn How To Find LCM of N Numbers in C Programming Language. LCM is an abbreviated form for Least Common Multiple. This Code To Calculate LCM of N Integers makes use of Functions, While Loop, For Loop and Modulus Operator.

#### What is LCM?

A Least Common Multiple (LCM) of Numbers is the Smallest Number which is a Multiple of all the Numbers given in a range.

#### Example of LCM

**LCM** of **4, 5** and **6** is **60.**

#### Formula To Calculate LCM of Two Numbers

**L.C.M = (x * y) / G.C.D**

**Must Read: Find LCM of Two Numbers in C Programming**

#### C Program To Find LCM of N Numbers using Functions and For Loop

#include<stdio.h> int gcdofnumbers(int val1, int val2) { int temp; if(val1 > val2) { temp = val1; val1 = val2; val2 = temp; } if(val2 % val1 == 0) { return val1; } else { return gcdofnumbers(val2 % val1, val1); } } int lcmofnumbers(int num1, int num2) { int gcd = gcdofnumbers(num1, num2); return (num1 * num2) / gcd; } int main() { int array[20], count, limit, value = 1; printf("\nEnter Total Number of Elements for LCM Calculation: \t"); scanf("%d", &limit); printf("\nEnter %d Numbers to Calculate LCM:\n", limit); for(count = 0; count < limit; count++) { scanf("%d", &array[count]); } for(count = 0; count < limit; count++) { value = lcmofnumbers(value, array[count]); } printf("\nLCM of given numbers = %d\n", value); return 0; }

**Must Read: C Program To Find Compound Interest**

#### C Program To Find LCM of N Numbers using Array and While Loops

#include<stdio.h> int gcdofnumbers(int val1, int val2) { int temp; if(val1 > val2) { temp = val1; val1 = val2; val2 = temp; } if(val2 % val1 == 0) { return val1; } else { return gcdofnumbers(val2 % val1, val1); } } int lcmofnumbers(int num1, int num2) { int gcd = gcdofnumbers(num1, num2); return (num1 * num2) / gcd; } int main() { int array[20], count, limit, value = 1; printf("\nEnter Total Number of Elements for LCM Calculation: \t"); scanf("%d", &limit); printf("\nEnter %d Numbers to Calculate LCM:\n", limit); count = 0; while(count < limit) { scanf("%d", &array[count]); count++; } count = 0; while(count < limit) { value = lcmofnumbers(value, array[count]); count++; } printf("\nLCM of given numbers = %d\n", value); return 0; }

**Must Read: C Program To Convert Celsius Temperature into Fahrenheit**

#### Output

In case you find any error in the above C Program Code To Calculate LCM of N Numbers or if you have any doubts, let us know about it in the Comment Section below.

Can we use malloc() function here to find lcm of n integers without Arrays?

Yes. You can use malloc() function.

You have written two differents methods of looping in finding lcm of n integers. I want to know is there any difference between While loop and For loop?

Apart from the syntax, there is not much difference in For and While Loops. At least for smaller inputs, there is no difference. However, in other languages such as C#, For loops tends to perform the execution faster compared to While loop.

Its a felling to me, when I open the C Program to construct a game. It means that there is no problem when I open the Program to find the LCM of n numbers even the logic. I thank you to upload your simply program. So next time I appeal you to try at best level to make difficulty.

thanks for the programs sir. 🙂

can you please tell me the error in this code

#include

main()

{

int n,i,max,count=0;

printf(“enter the number”);

scanf(“%d”,&n);

int a[n];

for(i=0;i<=n;i++)

{

scanf("%d",&a[i]);

}

max=a[0];

for(i=0;i<=n;i++)

{

if(max<=a[i])

max=a[i];

}

int count2=0;

for(i=0;i<=n;i++)

{

count=count+1;

for(j=0;j<=n;j++)

{

if(max%a[j]==0)

++count2;

}

if(count2==n)

break;

else

max=max*count;

}

printf("lcm is %d",max);

}

Hi, I do not see any point in just changing the looping type and writing the same program all over again. Also the code to find the GCD is very long and performs unnecessary actions. Rather you can refer to this article

https://www.geeksforgeeks.org/lcm-of-given-array-elements/