C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Find LCM of two numbers (loop)

Introduction

The LCM (Least Common Multiple) of two numbers is the smallest positive number that is divisible by both numbers.

 Examples:

  • LCM of 12 and 18 → 36
  • LCM of 5 and 10 → 10

This program calculates the LCM using a for, while and do..while loop.

 

C Program: Find LCM of two numbers (loop)

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int num1, num2, max, lcm, i;

 

    // Input

    printf("Enter two positive integers: ");

    scanf("%d %d", &num1, &num2);

 

    // Validate input

    if (num1 <= 0 || num2 <= 0) {

        printf("Invalid input! Please enter positive integers.\n");

        return 1;

    }

 

    // Start from the maximum of num1 and num2

    max = (num1 > num2) ? num1 : num2;

 

    // Find LCM using for loop

    for (i = max;; i++) { // Infinite loop, break when LCM found

        if (i % num1 == 0 && i % num2 == 0) {

            lcm = i;

            break;

        }

    }

 

    printf("LCM of %d and %d is: %d\n", num1, num2, lcm);

    return 0;

}

Output

 
OUTPUT 1 :
Enter two positive integers: 12 18
LCM of 12 and 18 is: 36

OUTPUT 2 :
Enter two positive integers: 5 10
LCM of 5 and 10 is: 10

Explanation

  1. User inputs two positive integers num1 and num2.
  2. Determine the maximum of num1 and num2 to start checking for LCM.
  3. Use a for loop starting from max and increment by 1.
  4. Check if the current number i is divisible by both num1 and num2.
  5. When found, assign to lcm and break the loop.
  6. Print the least common multiple.

 

C Program: Find LCM of two numbers (loop)

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int num1, num2, max, lcm;

 

    // Input

    printf("Enter two positive integers: ");

    scanf("%d %d", &num1, &num2);

 

    // Validate input

    if (num1 <= 0 || num2 <= 0) {

        printf("Invalid input! Please enter positive integers.\n");

        return 1;

    }

 

    // Start from the maximum of num1 and num2

    max = (num1 > num2) ? num1 : num2;

    lcm = max;

 

    // Find LCM using while loop

    while (1) { // Infinite loop, break when LCM found

        if (lcm % num1 == 0 && lcm % num2 == 0) {

            break;

        }

        lcm++;

    }

 

    printf("LCM of %d and %d is: %d\n", num1, num2, lcm);

    return 0;

}

Output

 
OUTPUT 1 :
Enter two positive integers: 12 18
LCM of 12 and 18 is: 36

OUTPUT 2 :
Enter two positive integers: 5 10
LCM of 5 and 10 is: 10

Explanation

  1. User inputs two positive integers num1 and num2.
  2. Determine the maximum of the two numbers to start checking for LCM.
  3. Initialize lcm = max.
  4. while loop checks if lcm is divisible by both numbers.
  5. If divisible, break the loop; otherwise increment lcm.
  6. Print the least common multiple.

 

C Program: Find LCM of two numbers (loop)

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int num1, num2, lcm;

 

    // Input

    printf("Enter two positive integers: ");

    scanf("%d %d", &num1, &num2);

 

    // Validate input

    if (num1 <= 0 || num2 <= 0) {

        printf("Invalid input! Please enter positive integers.\n");

        return 1;

    }

 

    // Start from the maximum of num1 and num2

    lcm = (num1 > num2) ? num1 : num2;

 

    // Find LCM using do...while loop

    do {

        if (lcm % num1 == 0 && lcm % num2 == 0) {

            break;

        }

        lcm++;

    } while (1); // Infinite loop until break

 

    printf("LCM of %d and %d is: %d\n", num1, num2, lcm);

    return 0;

}

Output

 
OUTPUT 1 :
Enter two positive integers: 12 18
LCM of 12 and 18 is: 36

OUTPUT 2 :
Enter two positive integers: 5 10
LCM of 5 and 10 is: 10

Explanation

  1. User inputs two positive integers num1 and num2.
  2. Initialize lcm as the maximum of num1 and num2.
  3. do..while loop checks if lcm is divisible by both numbers.
  4. If divisible, break the loop; otherwise increment lcm.
  5. Ensures at least one execution of the loop.
  6. Print the least common multiple.