Q:

Rust program to calculate the LCM using recursion

belongs to collection: Rust Functions Programs

0

In this program, we will create a recursive function to calculate the LCM and return the result to the calling function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to calculate the LCM using recursion is given below. The given program is compiled and executed successfully.

// Rust program to calculate the 
// LCM using recursion

unsafe fn LCM(a:i32, b:i32)->i32
{ 
    static mut res:i32 = 1;
 
    if (res % a == 0 && res % b == 0)
    {
        return res;
    }
    res = res + 1;

    LCM(a, b);

    return res;
}

fn main() {
    unsafe{
        let a:i32=45;
        let b:i32=75;
        
        let res = LCM(a, b);
        println!("The LCM of {0} and {1} is {2}.", a, b, res);
    }
}

Output:

The LCM of 45 and 75 is 225.

Explanation:

In the above program, we created two functions LCM() and main(). The LCM() function is a recursive function, which is used to calculate the LCM of two numbers and return the result to the calling function.

In the main() function, we called LCM() function and printed the.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to calculate the GCD using recursion... >>
<< Rust program to check a given number is prime or n...