How to calculate the LCM (Least Common Multiple) of two numbers in Rust

2 Answers

0 votes
#![allow(non_snake_case)]
#![allow(while_true)]

fn getLMC(a : i32, b : i32) -> i32 {
    let mut lmc = if a > b {a} else {b};
    
    while true {
        if lmc % a == 0 && lmc % b == 0 {
            return lmc;
        }
        lmc += 1;
    }
    
    return 0;
}

fn main() {
    let a = 12;
    let b = 20;
    
    println!("The LCM (Least Common Multiple) of {} and {} is: {}", a, b, getLMC(a, b));
}



  
/*
run:
  
The LCM (Least Common Multiple) of 12 and 20 is: 60
  
*/

 



answered Apr 27, 2023 by avibootz
0 votes
use num::integer::lcm;

fn main() {
    let a = 12;
    let b = 20;
    let result = lcm(a, b);
    
    println!("LCM of {} and {} is {}", a, b, result);
}
 
 
 
/*
run:
 
LCM of 12 and 20 is 60
 
*/

 



answered Apr 27, 2023 by avibootz
...