C

LCM of Two Numbers in C

In this tutorial, we are going to see how to find LCM of two numbers in C. The Least Common Multiple – LCM of two integers n1 and n2 is the smallest positive integer perfectly divisible by n1 and n2 (without remainder). For example:
 


 
GCD Program in CGCD Program in CIn this tutorial, we are going to see how to write a GCD program in C. The GCD or the Greatest Common Divisor of two…Read More
 

LCM of Two Numbers in C
#include <stdio.h>

int main()
{
    int nbr1, nbr2, lcm;
  
    printf("Enter two numbers: ");
    scanf("%d %d", &nbr1, &nbr2);
  
    lcm = (nbr1 > nbr2) ? nbr1 : nbr2;
  
    // Always true
    while(1)
    {
        if( lcm%nbr1==0 && lcm%nbr2==0 )
        {
            printf("LCM of %d and %d = %d", nbr1, nbr2, lcm);
            break;
        }
        ++lcm;
    }
    return 0;
}

Output:

Enter two numbers: 30 45
LCM of 30 and 45 = 90

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *