In this tutorial you will learn about the C Program to Find LCM of n Numbers and its application with practical example.
C Program to Find LCM of n Numbers
In this tutorial, we will learn to create a C program that will Find LCMof n numbers using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- While loop in C programming.
- Arithmetic operations in C Programming.
Program to Find LCM of n numbers
In c programming, it is possible to take numerical input from the user and find LCM of n numbers with the help of a very small amount of code. The C language has many types of header libraries which has supported function in them with the help of these files the programming is easy.
With the help of this program, we can Find LCM of n numbers
Here,
LCM:- Least Common Multiple.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declaring the variables for the program. 2. Taking the input number from the user. 3. Passing to the loop. 4. Calculating the <strong>LCM</strong>. 5. Printing the result numbers. |
Program to Find LCM of n numbers:-
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 |
#include<stdio.h> int main() { //declaring the variable for the programs int arr[10], n, mp, i, count; //taking the input size from the user printf("Enter the Size: "); scanf("%d", &n); //taking the input elements from the user printf("Enter %d Numbers: ", n); for(i=0; i<n; i++) scanf("%d", &arr[i]); i=0; mp = arr[i]; //Calculating the LCM while(i<n) { if(mp<arr[i]) mp = arr[i]; i++; } while(1) { i=0; count=0; while(i<n) { if(mp%arr[i]==0) count++; i++; } if(count==n) break; else mp++; } //Printing the LCM printf("\nLCM("); for(i=0; i<(n-1); i++) printf("%d,", arr[i]); printf("%d) = %d", arr[i], mp); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- i = it will hold the integer value.
- mp = it will hold the integer value.
- arr[10] = it will hold the integer value.
- n = it will hold the integer value.
- count = it will hold the temporary integer value.
Input number from the user.
Calculating the LCM of the given numbers.
Printing the LCM of the given numbers.