Rust program to demonstrate the nested loop statement

Rust | Nested loop statement Example: Using the loop statement, we have to print the tables from 2 to 5.
Submitted by Nidhi, on October 05, 2021

Problem Solution:

In this program, we will use a nested loop statement to print tables from 2 to 5.

Program/Source Code:

The source code to demonstrate the nested loop statement is given below. The given program is compiled and executed successfully.

// Rust program to demonstrate the 
// nested "loop" statement

fn main() {
    let mut cnt1:i32 = 2;
    let mut cnt2:i32 = 0;
    
    loop
    {
        if(cnt1>5)
        {
            break;
        }
        
        cnt2=1;
        loop
        {
            if(cnt2>10)
            {
                break;
            }
            print!("{} ",(cnt1*cnt2));
            cnt2=cnt2+1;
        }
        cnt1=cnt1+1;
        println!();
    }
}

Output:

2 4 6 8 10 12 14 16 18 20 
3 6 9 12 15 18 21 24 27 30 
4 8 12 16 20 24 28 32 36 40 
5 10 15 20 25 30 35 40 45 50 

Explanation:

Here, we used a nested loop statement to print tables from 2 to 5.

Rust Looping Programs »





Comments and Discussions!

Load comments ↻





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