Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Array

Patterns

Excersises


C Program to Find LCM of two numbers


In this program, we are getting two positive integers as input from the user and find the LCM of it.

What is LCM ?

LCM stands for least common multiple. The least common multiple is the smallest multiple that two or more numbers have in common.

How to Find LCM ?

For Example we take two integers,6 and 8.

  • Multiples of 5 is : 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78...
  • Multiples of 10 is : 8, 16, 24, 32, 40, 48, 56, 64, 72, 80...
24,48 and 72 are common multiples of 6 and 8. The smallest multiple is 24. Therefore, 24 is the least common multiple (LCM) of 6 and 8.

Example : pgm.c

#include<stdio.h>
void main()
{
    int i,n1,n2,max,lcm;

    printf("Enter 2 Positive Integers : ");
    scanf("%d%d",&n1,&n2);

    //Store the common multiple of n1 & n2 into the max variable.
    max=n1>n2?n1:n2;
    i=max;
    lcm=1;

    while(1)
    {
        if (i%n1==0 && i%n2==0)
        {
            lcm=i;
            break;
        }
        i+=lcm;
    }
    printf("LCM of %d and %d is %d",n1,n2,lcm);
}
Output:
Enter 2 Positive Integers : 6 8
LCM of 6 and 8 is 24