C program to find lcm of two numbers

devquora
devquora

Posted On: Dec 23, 2020

C program to find lcm of two numbers

 

Overview:

In this article I will show you hopw to find the GCD of two numbers with examples and a C Program. THis article include the following Table of Contents as follows:

Table of contents:

  1. What is a Number?
  2. What do you mean by factors?
  3. What do you mean by multiple?
  4. What is LCM?
  5. Demonstration for LCM of two numbers
  6. How to find LCM of two numbers?
  7. C program to find lcm of two numbers
  8. Conclusion

What is a Number?

Number is an object that uses digits to perform mathematical task. Calculus that is a branch of mathematics included many numbers such as integer, whole number, real number, and imaginary numbers so on. Here, I will discuss about number to perform arithmetic addition operation. Moreover, Number is a combination of digits, some symbols and decimal point. For instance, 12 and 30 are numbers.

What do you mean by factors?

Factors are numbers or quantity that when multiplied with one another produces a given number or expression. For instance, 2, 2, 3 are the factors of 12 and 2, 3, 5 are the factors of 30.

What do you mean by multiple?

A multiple of a number is the product of that number and an integer. For example, 10 is a multiple of 5 because 5 × 2 = 10, so 10 is divisible by 5 and 2.

Multiple of 4 are:

4, 8, 12, 16, 20, 24, 28, 32, 36, 40.....

Multiple of 6 are:

6, 12, 18, 24, 30, 36, 42, 48......

Common multiple of 4 and 6 are 12, 24, 36....

The least common multiple is 12 so lcm(4,6)=12

 

What is LCM?

LCM is a smallest positive integer that exactly divides two or more numbers. 

The LCM (Least Common Multiple or Lowest common multiple) of two integers is the smallest positive integer that is perfectly divisible by both numbers (without a remainder).

For example, the LCM of 72 and 120 is 360. 

The Factor of  72 = 2*2*2*3*3

The Factor of 120 = 2*2*2*3*5

Then the least common factor of 72 and 120 is 2*2*2*3*3*5 = 360. 

Other example,

The LCM of 12 and 64 is 360. 

The Factors of  12 = 2*2*3

The Factors of 64 = 2*2*2*2*2*2

Then the least common Multiple of 72 and 120 is 2*2*2*2*2*2*3 = 192. 

On the other hand the LCM of two integers a and b, usually denoted by lcm(a, b), is the smallest positive integer that is divisible by both a and b.

How to find LCM of two numbers?

  • Step 1: Write the multipliers of both two numbers.
  • Step 2: Take out the common as well as left factors.
  • Step 3: Then to again multiply these common  and left factors, which is the lcm of the given two numbers.

Demonstration for GCD of two numbers

Demo of LCM

Demo of LCM

C program to find lcm of two numbers

#include <stdio.h>
int main()
{
    int i, num1, num2, max, lcm=1;
    printf("Enter any two numbers to find LCM: ");
    scanf("%d", &num1);
	printf("Enter any two numbers to find LCM: ");
    scanf("%d", &num2);
    /* Find maximum between num1 and num2 */
    max = (num1 > num2) ? num1 : num2;
    /* First multiple to be checked */
    i = max;
    /* Run loop indefinitely till LCM is found */
    while(1)
    {
        if(i%num1==0 && i%num2==0)
        {
            /*If 'i' divides both 'num1' and 'num2'
             * then 'i' is the LCM.
             */
            lcm = i;

            /* Terminate the loop after LCM is found */
            break;
        }
        /*
         * If LCM is not found then generate next multiple of max between both numbers */
        i += max;
    }
    printf("LCM of %d and %d = %d", num1, num2, lcm);
    return 0;
}

The output of the Program:

Demo of Reverse Number

Conclusion:

This Program stores two integers one by one into the int datatype. After that find the  least common multiple of these two numbers as shown as output.

    Related Questions

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse a string

    This program reversed the entered string that means opposite of the previous string sequence...

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..