Utopper SkillUtopper Skill
  • Programming
    • Programming Examples
  • Interview Questions
    • DevOps Interview Questions
    • Android Interview Questions
  • How to
  • Tools
  • Top 10
  • Book Summaries
Reading: C Program To Find LCM of Two Numbers
Share
Utopper SkillUtopper Skill
Search
  • Book Summaries
  • Programming Examples
  • C Programming Example
  • Interview Question
  • How to
  • Top 10
Follow US
Utopper Skill > Programming Examples > C Programming Example > C Program To Find LCM of Two Numbers
Programming ExamplesC Programming Example

C Program To Find LCM of Two Numbers

Utopper Skill Team
By Utopper Skill Team Last updated: March 28, 2023
Share
6 Min Read
C Program To Find LCM of Two Numbers
SHARE
Table of Content
What is LCM?How to find LCM in Maths?Pseudo Code to Find LCM of Two Numbers using C ProgramApproach to Find LCM of Two Numbers using C ProgramC Program To Find LCM of Two NumbersOutput

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
TAGGED:C Program Exampleslcm of two numbers
Share This Article
Facebook Twitter Whatsapp Whatsapp Telegram Copy Link Print
Previous Article Book Summary of The Psychology of Money Book Summary of The Psychology of Money by Morgan Housel
Next Article 14 Best CRM Software for Your Business Operations
Most Popular
Book Summary of Ikigai by Hector Garcia
Book Summary of Ikigai by Hector Garcia
Utopper Skill Team By Utopper Skill Team
Top 40 HTML Interview Questions With Detailed Answers 2024
Top 40 HTML Interview Questions With Detailed Answers 2024
Utopper Skill Author By Utopper Skill Author
Learn C Programming (Basic to Advanced)
C Environmental Setup
Utopper Skill Author By Utopper Skill Author
Kubernetes Interview Questions and Answers
Top 40 Kubernetes Interview Questions and Answers 2024 Edition
Utopper Skill Team By Utopper Skill Team
Learn C Programming (Basic to Advanced)
ftell() in C Programming
Utopper Skill Author By Utopper Skill Author

You Might Also Like

C++ Program To Print Prime Numbers From 1 To N
C Programming ExampleProgramming Examples

C++ Program To Print Prime Numbers From 1 To N

11 Min Read
C Program to Print Your Own Name
Programming ExamplesC Programming Example

C Program to Print Your Own Name with Example

2 Min Read
C Program To Convert Fahrenheit To Celsius
C Programming ExampleProgramming Examples

C Program To Convert Fahrenheit To Celsius

5 Min Read
C++ Program To Add Two Complex Numbers
Programming ExamplesC++ Programming Example

C++ Program To Add Two Complex Numbers

8 Min Read

Mail : [email protected]

Privacy Policy | DISCLAIMER | Contact Us

Learn

  • learn HTML
  • learn CSS
  • learn JavaScript

Examples

  • C Examples
  • C++ Examples
  • Java Examples

Study Material

  • Interview Questions
  • How to
  • Hosting
  • SEO
  • Blogging

 

© 2024 : UTOPPER.COM | Owned By : GROWTH EDUCATION SOLUTIONS PRIVATE LIMITED
Welcome Back!

Sign in to your account

Lost your password?