Calculate LCM of Two Numbers in C

Exercise:

Write a C program to find LCM of two numbers.

1. Calculate using while and if

Click Here to View the Solution!
#include <stdio.h>
int main() {
    int num1, num2, max;
    printf("Enter two positive integers: ");
    scanf("%d %d", &num1, &num2);

    // maximum number between n1 and n2 is stored in min
    max = (num1 > num2) ? num1 : num2;
    while (1) {
        if (max % num1 == 0 && max % num2 == 0) {
            printf("The LCM of %d and %d is %d.", num1, num2, max);
            break;
        }
        ++max;
    }
    return 0;
}
Click Here to View the Output!
Enter two positive integers: 7 3
The LCM of 7 and 3 is 21.
Click Here to View the Explanation!
  • This program is used to find out the LCM of two integers using a while loop and an if statement.
  • In main(), the program requests the user to enter two integers and stores them into the variables num1 and num2 using the scanf() function.
  • A ternary operator is used which checks which integer among num1 and num2 is the largest and stores it into the variable max the reason being, the value of LCM can never be smaller than max.
  • An infinite while loop is initialized which holds an if statement which checks whether max is exactly divisible by num1 and num2. If true, a message is printed stating the LCM.
  • If false, the value of max is incremented by one and the iterations continue until the condition of the if statement is true and the LCM is found.
  • The return 0 statement is used to exit the program execution.

2. Calculate using GCD

Click Here to View the Solution!
#include <stdio.h>
int main() {
    int num1, num2, i, gcd, lcm;
    printf("Enter two positive integers: ");
    scanf("%d %d", &num1, &num2);

    for (i = 1; i <= num1 && i <= num2; ++i) {
        // check if i is a factor of num1 and num2
        if (num1 % i == 0 && num2 % i == 0)
        gcd = i;
    }
    lcm = (num1 * num2) / gcd;
    printf("The LCM of two numbers %d and %d is %d.", num1, num2, lcm);
    return 0;
}
Click Here to View the Output!
Enter two positive integers: 8 3
The LCM of two numbers 8 and 3 is 24.
Click Here to View the Explanation!
  • This program is used to find out the LCM of two numbers using the GCD of the two integers.
  • In main(), the program requests the user to enter two integer values and stores them in the integer variables num1 and num2 using the scanf() function.
  • A for loop is initialized that will continue the iterations of the loop until the value of i is less than or equal to num1 and num2.
  • Every iteration will determine whether i is exactly divisible by both the integers num1 and num2 (factors).
  • If the condition is true, the value of i will be stored in an integer variable gcd.
  • The loop will continue until the greatest common divisor of num1 and num2 is found and stored in the variable gcd.
  • A formula (num1 * num2) / gcd is used to calculate the LCM of the two integers and the result is displayed on the screen.
  • The return 0 statement is used to exit the program execution.
%d bloggers like this: