Skip to content
Utopper skill
  • ProgrammingExpand
    • Programming Examples
  • Interview QuestionsExpand
    • DevOps Interview Questions
    • Android Interview Questions
  • How to
  • Tools
  • Top 10
  • Book Summaries
Utopper skill
Programming Examples C Programming Example

C Program To Find LCM of Two Numbers

Article Contents

  1. What is LCM?
  2. How to find LCM in Maths?
  3. Pseudo Code to Find LCM of Two Numbers using C Program
  4. Approach to Find LCM of Two Numbers using C Program
  5. C Program To Find LCM of Two Numbers
  6. Output

What is LCM?

LCM means “Least Common Multiple”. This mathematical idea finds the smallest positive integer that is a multiple of two or more integers. In other terms, the LCM of two numbers is the least number divisible by all of them.

12 is the lowest positive integer that is a multiple of both 3 and 4, hence it is the LCM of 3 and 4. 30, the smallest positive integer divisible by all three numbers, is the LCM of 2, 3, and 5.

LCM is significant in number theory, algebra, and calculus. Practical uses include calculating the time it takes two or more objects to accomplish a task or determining the frequency of events at varied intervals.

C Program To Find LCM of Two Numbers
C Program To Find LCM of Two Numbers

How to find LCM in Maths?

To find the LCM (Least Common Multiple) of two or more numbers in mathematics, you can follow these steps:

  1. Write down the prime factorization of each number.
  2. Identify all the unique prime factors from the prime factorizations.
  3. For each unique prime factor, find the highest power of that factor that appears in any of the prime factorizations.
  4. Multiply all the prime factors together, each raised to the highest power found in step 3. The result is the LCM.

For example, let’s find the LCM of 12 and 18:

  1. Prime factorization of 12: 2 x 2 x 3 Prime factorization of 18: 2 x 3 x 3
  2. Unique prime factors: 2, 3
  3. Highest power of 2: 2 x 2 = 4 Highest power of 3: 3 x 3 = 9
  4. LCM = 2^2 x 3^2 = 36

Therefore, the LCM of 12 and 18 is 36.

Click here to Read Other C Examples

Pseudo Code to Find LCM of Two Numbers using C Program

// Declare variables to store the two input numbers and the LCM
int num1, num2, lcm;

// Ask the user to input the two numbers
print("Enter the first number: ")
read num1
print("Enter the second number: ")
read num2

// Find the maximum number between num1 and num2
if num1 > num2 then
    lcm = num1
else
    lcm = num2

// Loop until a common multiple is found
while true do
    // Check if lcm is a multiple of both num1 and num2
    if lcm % num1 == 0 and lcm % num2 == 0 then
        // lcm is the LCM, print it and break out of the loop
        print("The LCM of", num1, "and", num2, "is", lcm)
        break
    end if
    // If lcm is not a common multiple, increment it by the maximum input value
    lcm = lcm + max(num1, num2)
end while

Approach to Find LCM of Two Numbers using C Program

To find the LCM (Least Common Multiple) of two numbers in C language, you can use the following steps:

  1. Ask the user to input the two numbers.
  2. Find the maximum number among the two numbers and assign it to a variable called “max”.
  3. Create a while loop that will continue until a common multiple is found.
  4. Inside the while loop, increment the value of “max” by the maximum value of the two numbers.
  5. Check if both numbers are divisible by the value of “max”. If they are, then “max” is the LCM and you can exit the loop.
  6. Outside the loop, print the value of “max” as the LCM.

C Program To Find LCM of Two Numbers

#include <stdio.h>

int main() {
    int num1, num2, max;

    printf("Enter the first number: ");
    scanf("%d", &num1);

    printf("Enter the second number: ");
    scanf("%d", &num2);

    // Find the maximum number between num1 and num2
    max = (num1 > num2) ? num1 : num2;

    // Loop until a common multiple is found
    while (1) {
        if (max % num1 == 0 && max % num2 == 0) {
            printf("The LCM of %d and %d is %d", num1, num2, max);
            break;
        }
        max++;
    }

    return 0;
}

Output

This program takes two integer inputs from the user and calculates their LCM using a loop. The output is displayed on the console with a message like “The LCM of 6 and 8 is 24”, depending on the user inputs.

Here’s an example output for input values of 6 and 8:

Enter the first number: 6
Enter the second number: 8
The LCM of 6 and 8 is 24
Post Tags: #C Program Examples#lcm of two numbers
Avatar photo
Utopper Skill Team
Facebook X Instagram YouTube Linkedin Pinterest

Post navigation

Previous Previous
Book Summary of The Psychology of Money by Morgan Housel
NextContinue
14 Best CRM Software for Your Business Operations

Latest Interview Questions

  • C++ Program To Print Prime Numbers From 1 To N
  • C Program to Print Your Own Name with Example
  • Book Summary of Why We Sleep By Matthew Walker
  • Book Summary of Indistractable By Nir Eyal
  • C Program To Convert Fahrenheit To Celsius
  • Book Summary of The One Thing By Gary Keller and Jay Papasan
  • Facebook
  • Instagram
  • YouTube
  • Telegram
  • LinkedIn
  • Twitter

Mail : [email protected]

Privacy Policy | DISCLAIMER | Contact Us

Learn Development

learn HTML

learn CSS

learn JavaScript

Examples

C Examples

C++ Examples

Java Examples

Study Material

Interview Questions

How to

Hosting

SEO

Blogging

© 2023 Utopper.com

All Rights Reserved with Copyright & Registered TradeMarks
OWNED BY : GROWTH EDUCATION SOLUTIONS PRIVATE LIMITED

Scroll to top
  • Programming
    • Programming Examples
  • Interview Questions
    • DevOps Interview Questions
    • Android Interview Questions
  • How to
  • Tools
  • Top 10
  • Book Summaries
Search