ADVERTISEMENT
ADVERTISEMENT

Rust program to find the LCM (Lowest Common Multiple)

Given two numbers, we have to find the LCM (Lowest Common Multiple) using Rust program.
Submitted by Nidhi, on September 29, 2021

Problem Solution:

Here, we will read two integer numbers from the user. Then we will find the Lowest Common Multiple and print the result.

Program/Source Code:

The source code to find the LCM (Lowest Common Multiple) is given below. The given program is compiled and executed successfully.

// Rust program to find the LCM 
// (Lowest Common Multiple)

use std::io;

fn main() 
{
    let mut n1:i32 = 0;
    let mut n2:i32 = 0;
    let mut rem:i32= 0;
    let mut lcm:i32= 0;
    let mut x:i32  = 0;
    let mut y:i32  = 0;
    
    let mut input1 = String::new();
    let mut input2 = String::new();
    
    println!("Enter Number1: ");
    io::stdin().read_line(&mut input1).expect("Not a valid string");
    n1 = input1.trim().parse().expect("Not a valid number");

    println!("Enter Number2: ");
    io::stdin().read_line(&mut input2).expect("Not a valid string");
    n2 = input2.trim().parse().expect("Not a valid number");
    
    if (n1 > n2) {
        x = n1;
        y = n2;
    }
    else {
        x = n2;
        y = n1;
    }

    rem = x % y;

    while (rem != 0) {
        x = y;
        y = rem;
        rem = x % y;
    }

    lcm = n1 * n2 / y;
    println!("Lowest Common Multiple is: {}", lcm);
}

Output:

RUN 1:
Enter Number1: 
30
Enter Number2: 
5
Lowest Common Multiple is: 30

RUN 2:
Enter Number1: 
10
Enter Number2: 
125
Lowest Common Multiple is: 250

Explanation:

Here, we read the value of n1, n2 from the user. Then we found the LCM (Lowest Common Multiple) of n1 and n2. After that, we printed the result.

Rust Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.