Rust program to demonstrate the ladder (multiple) if-else statements

Rust | Multiple if-else statements example: Given three numbers, we have to check the largest among three numbers.
Submitted by Nidhi, on October 03, 2021

Problem Solution:

Here, we are creating three integer variables and then find the largest number among them using the multiple (ladder) if-else statements.

Program/Source Code:

The source code to demonstrate the multiple (ladder) if-else statements is given below. The given program is compiled and executed successfully.

// Rust program to demonstrate the 
// multiple (ladder) if-else statements

fn main() {
    let mut num1:i32 = 95;
    let mut num2:i32 = 105;
    let mut num3:i32 = 75;
    
    let mut large:i32=0;

    if(num1>num2 && num1>num3)
    {
        large=num1;
    }
    else if(num2>num1 && num2>num3)
    {
        large=num2;
    }
    else
    {
        large=num3;
    }
    
    println!("Largest number is: {}",large);
}

Output:

Largest number is: 105

Explanation:

Here, we created 4 integer variables num1, num2, num3, and large. Those are initialized with 95, 105, 75, and 0 respectively. Then we checked and found the largest number using multiple if-else statements and printed the result.

Rust if/else Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.