How to multiply two numbers without using the multiple operator (*) in Rust

1 Answer

0 votes
pub fn multiply(a: i32, b: i32) -> i32 {
    let mut mul = 0;

    for _ in 1..=a {
        mul += b;
    }

    mul
}

fn main() {
    let a = 4;
    let b = 9;

    println!("{} * {} = {}", a, b, multiply(a, b));
}



  
/*
run:

4 * 9 = 36

*/

 



answered Aug 31, 2024 by avibootz
...