Program to Find LCM of Two Numbers using While loop

Program to Find LCM of Two Numbers using While loop

  • Write a program to Find LCM Two Numbers using While loop in C
  • Write a program to Find LCM Two Numbers using While loop in C++
  • Write a program to Find LCM Two Numbers using While loop in Python
  • Write a program to Find LCM Two Numbers using While loop in PHP
  • Write a program to Find LCM Two Numbers using While loop in Java
  • Write a program to Find LCM Two Numbers using While loop in Java Script
  • Write a program to Find LCM Two Numbers using While loop in C#

Explanation:

o find the LCM (Least Common Multiple) of two numbers using a While loop, the logic is:

Logic:

  1. Definition of LCM:
    • The LCM of two numbers is the smallest positive number that is a multiple of both numbers.
  2. Start with the Larger Number:
    • The LCM cannot be smaller than the larger of the two numbers.
  3. Iterate Through Multiples:
    • Use a While loop to check successive multiples of the larger number.
    • If a multiple is divisible by the smaller number, it’s the LCM.

Program to Find LCM of Two Numbers using While loop

#include <stdio.h>

int main() {
    int a, b, lcm, max;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    max = (a > b) ? a : b;
    lcm = max;

    while (1) {
        if (lcm % a == 0 && lcm % b == 0) {
            printf("LCM is %d\n", lcm);
            break;
        }
        lcm++;
    }

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int a, b, lcm, max;
    cout << "Enter two numbers: ";
    cin >> a >> b;

    max = (a > b) ? a : b;
    lcm = max;

    while (true) {
        if (lcm % a == 0 && lcm % b == 0) {
            cout << "LCM is " << lcm << endl;
            break;
        }
        lcm++;
    }

    return 0;
}

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

lcm = max(a, b)

while True:
    if lcm % a == 0 and lcm % b == 0:
        print(f"LCM is {lcm}")
        break
    lcm += 1

<?php
$a = intval(readline("Enter first number: "));
$b = intval(readline("Enter second number: "));

$lcm = max($a, $b);

while (true) {
    if ($lcm % $a == 0 && $lcm % $b == 0) {
        echo "LCM is $lcm\n";
        break;
    }
    $lcm++;
}
?>

import java.util.Scanner;

public class LCM {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter first number: ");
        int a = sc.nextInt();
        System.out.print("Enter second number: ");
        int b = sc.nextInt();

        int lcm = Math.max(a, b);

        while (true) {
            if (lcm % a == 0 && lcm % b == 0) {
                System.out.println("LCM is " + lcm);
                break;
            }
            lcm++;
        }
    }
}

let a = parseInt(prompt("Enter first number: "));
let b = parseInt(prompt("Enter second number: "));

let lcm = Math.max(a, b);

while (true) {
    if (lcm % a === 0 && lcm % b === 0) {
        console.log("LCM is " + lcm);
        break;
    }
    lcm++;
}

using System;

class Program {
    static void Main() {
        Console.Write("Enter first number: ");
        int a = int.Parse(Console.ReadLine());
        Console.Write("Enter second number: ");
        int b = int.Parse(Console.ReadLine());

        int lcm = Math.Max(a, b);

        while (true) {
            if (lcm % a == 0 && lcm % b == 0) {
                Console.WriteLine("LCM is " + lcm);
                break;
            }
            lcm++;
        }
    }
}

List of All Programs