C Program for LCM Of Two Numbers

LCM of two numbers

Here, in this page we will dicuss the LCM of two numbers in C . The Least Common Multiple (LCM) is also referred to as the Lowest Common Multiple (LCM) and Least Common Denominator (LCD). The least common multiple, or LCM, is another number that’s useful in solving many math problems. Let’s find the LCM of 12 and 44. One way to find the least common multiple of two numbers is to first list the prime factors of each number.

    12 = 2 × 2 × 3

    44 = 2 × 2 × 11

A C program can calculate the Lowest Common Multiple (LCM) of two numbersThe method includes finding out the maximum values among two numbers, which are common in both the numbers. The algorithm below will help to calculate the LCM of two numbers. 

LCM

Working:-

  • Take two integers value from the user and store them in variable say n1 and n2.
  • Create a function say lcm_two_no() that takes two integers value as parameters.
  • Now, in the function lcm_two_no(), Declare two variables say check1 and check2.
  • Copy the value of n1 and n2 of variable to check1 and check2 variables respectively.
  • Initialize the while loop where condition is while(check1!=check2).
  • In while loop there are two condition If check1<check2
  • If condition is true then increment the check1 variable by n1 value, i.e, check1=check1+n1; .
  • Otherwise increment the check2 variable by n2 i.e,
    check2=check2+n2; .
  • Print the value of check1 or check2.
LCM of two numbers in C
Competitive Coding Techniques

C Program to calculate LCM of two numbers.  

/** C program to calculate the LCM of two numbers */
#include<stdio.h>
void lcm_two_no(int,int);
int main()
{
int n1,n2;
//to take user input n1,n2
printf("Enter two numbers: ");
scanf("%d %d",&n1,&n2);
//call of user define function
lcm_two_no(n1,n2);
return 0;
}
//function to calculate l.c.m
void lcm_two_no(int n1,int n2)
{
int check1,check2;
//to use of duplicity value
check1=n1;
check2=n2;
//to find lcm of number
while(check1!=check2)
{
//for condition true
if(check1< check2)
check1=check1+n1;
//for condition false
else
check2=check2+n2;
}
printf("\nL.C.M of %d and %d is: %d",n1,n2,check1);
}

 

Output

Enter any two numbers: 12 

30 

LCM of 12 and 30 is 60.