Program to Find LCM of Two Numbers using For loop
- Write a program to Find LCM Two Numbers using For loop in C
- Write a program to Find LCM Two Numbers using For loop in C++
- Write a program to Find LCM Two Numbers using For loop in Python
- Write a program to Find LCM Two Numbers using For loop in PHP
- Write a program to Find LCM Two Numbers using For loop in Java
- Write a program to Find LCM Two Numbers using For loop in Java Script
- Write a program to Find LCM Two Numbers using For loop in C#
Explanation:
o find the LCM (Least Common Multiple) of two numbers using a for loop, the logic is:
Logic:
- Definition of LCM:
- The LCM of two numbers is the smallest positive number that is a multiple of both numbers.
- Start with the Larger Number:
- The LCM cannot be smaller than the larger of the two numbers.
- Iterate Through Multiples:
- Use a for 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 For loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
int main() {
int a, b, lcm;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
int max = (a > b) ? a : b;
for (lcm = max; ; lcm++) {
if (lcm % a == 0 && lcm % b == 0) {
break;
}
}
printf("LCM of %d and %d is %d\n", a, b, lcm);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b, lcm;
cout << "Enter two numbers: ";
cin >> a >> b;
int max = (a > b) ? a : b;
for (lcm = max; ; lcm++) {
if (lcm % a == 0 && lcm % b == 0) {
break;
}
}
cout << "LCM of " << a << " and " << b << " is " << lcm << endl;
return 0;
}
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
max_val = max(a, b)
for lcm in range(max_val, a * b + 1):
if lcm % a == 0 and lcm % b == 0:
break
print(f"LCM of {a} and {b} is {lcm}")
<?php
$a = (int)readline("Enter the first number: ");
$b = (int)readline("Enter the second number: ");
$max = max($a, $b);
for ($lcm = $max; ; $lcm++) {
if ($lcm % $a == 0 && $lcm % $b == 0) {
break;
}
}
echo "LCM of $a and $b is $lcm\n";
?>
import java.util.Scanner;
public class LCM {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
int a = sc.nextInt();
System.out.print("Enter the second number: ");
int b = sc.nextInt();
int max = Math.max(a, b);
int lcm = max;
for (; ; lcm++) {
if (lcm % a == 0 && lcm % b == 0) {
break;
}
}
System.out.println("LCM of " + a + " and " + b + " is " + lcm);
sc.close();
}
}
let a = parseInt(prompt("Enter the first number: "));
let b = parseInt(prompt("Enter the second number: "));
let max = Math.max(a, b);
let lcm = max;
for (; ; lcm++) {
if (lcm % a === 0 && lcm % b === 0) {
break;
}
}
console.log(`LCM of ${a} and ${b} is ${lcm}`);
using System;
class Program {
static void Main() {
Console.Write("Enter the first number: ");
int a = int.Parse(Console.ReadLine());
Console.Write("Enter the second number: ");
int b = int.Parse(Console.ReadLine());
int max = Math.Max(a, b);
int lcm = max;
for (; ; lcm++) {
if (lcm % a == 0 && lcm % b == 0) {
break;
}
}
Console.WriteLine($"LCM of {a} and {b} is {lcm}");
}
}