Program to find the LCM of Two numbers in C Language

Program-to-find-the-LCM-of-Two-numbers-in-C-Language

Program Description:

Write a Program to calculate the Lowest Common Multiple or LCM of Two numbers in C programming language. The program should accept two positive numbers from the user and calculate the LCM of the given numbers. Program should also perform the error checks and display error message on Invalid Input.

Before going to write the program. It is necessary to understand what is LCM and how to calculate the LCM.

What is LCM or Lowest Common Multiple?

The LCM or Lowest Common Mulitple of two numbers num1 and num2 is the smallest Integer that is perfectly (Evenly) divisible by both num1 and num2.

📢 The LCM is the smallest positive integer that is evenly divisible by both num1 and num2.

For example,

LCM(4, 30) = 60

LCM(6,10) = 30

There are couple of ways to calculate the LCM of two numbers, There are

  • We can calculate the LCM of two numbers by manually calculating the lowest multiple which divisible by both num1 and num2.
  • Using the GCD or Greatest Common Divisor of two numbers. – Here is the formula to calculate the LCM based on GCD.
    • LCM = (num1 * num2) / GCD

We are going to look at both programs in this tutorial.

Program Example Input and Output:

Input:

Enter two positve numbers to calculate LCM: 30 20

Output:

LCM of 30 and 20 is 60

Pre-Requisites:

We need to have the knowledge of the C Loops and Arithmetic operators like Modulus operators. Please go through following articles to understand more about them

LCM of Two numbers in C Program Algorithm:

  1. Accept the input of two numbers from the user and store them in variables num1 and num2
  2. Check for negative numbers and display an error message if the user enters a negative number.
  3. Calculate the maximum of two numbers ( num1 and num2). We are using Conditional operator to calculate the maximum. Store the maximum in max variable
  4. Start the Infinite Loop and Check if the max is evenly divisible by num1 and num2 using ((max % num1 == 0) && (max % num2 == 0)).
    • If the max is evenly divisible without any remainder, Then max is our LCM. Print it and break the loop.
    • max is not perfectly divisible by both numbers num1 and num2. Increment the max by 1 using max++. Go to next iteration.
  5. The above step 4 will continue until we get the LCM or Lowest Common Multiple. Once we break out of the Infinite loop, The variable max contain the LCM of num1 and num2

Program to calculate the LCM of Two numbers in C using while loop:

We are going to use the Infinite while loop with the while(1) syntax.

Program Output:

Save the program using .c extension and compile and run program. We are using GCC compiler to compile the program

Let’s check the positive number test cases.

As we can see from the above output, We are getting the desired result and program is able to calculate the LCM of given numbers.

Let’s try the negative numbers test case. As the LCM is only for positive numbers, The program should display an error message

program-to-calculate-LCM-of-two-numbers-output

As shown in the output from the program, whenever a user enters a negative number, an error message is displayed. Also take note that the program displays an error if the user enters a negative integer for either num1 or num2.

Find the LCM of Two numbers in C Language using for loop:

Let’s re-write the above program using the for loop. We can create the infinite loops using the for loop using the following for loop syntax.

for (; ; )

If you don’t specify any condition to for loop, Then for loop will run forever. Thus creating Infinite loop.

Here is the LCM program using the for loop in C.

Program Output:

The program is giving the valid output and also showing the error message on Invalid input.

LCM of Two numbers in C language using GCD:

We can also find the LCM of two numbers  num1 and  num2 using their GCD or Greatest Common Divisor using following formula.

LCM = (num1 * num2) / GCD

Let’s use the above formula to calculate the LCM using GCD formula.

We have already covered the Program to calculate the GCD of Two Numbers in C in earlier posts. We are going to use same logic to calculate the GCD in this program.

Program Output:

Let’s try positive number test cases and negative number test cases and see if we are getting the desired output.

LCM-Program-in-C-language

Test Case 1: Test with Positive numbers:

We are getting the LCM of two numbers correctly.

Test Case 2: Test with Negative numbers

As you can see from the above output, Program is displaying the error message.

Learn More about LCM at Wiki:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. […] have looked at the LCM of two numbers program in our earlier articles, In today’s article, We are going to write a program to check […]

  2. […] C Program to Calculate the LCM of two Numbers […]

  3. […] C Program to Calculate the LCM of two Numbers […]

Leave a Reply