# Find LCM of N Numbers C Program

## 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #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.