Rust program to print numbers from 1 to 10 using while, for loop, and loop

Rust | while, for, and loop examples: By using these looping statements we have to print the numbers between 1 to 10.
Submitted by Nidhi, on October 05, 2021

Problem Solution:

In this program, we will print numbers from 1 to 10 using while, for loop and loop statements.

Program/Source Code:

The source code to print numbers from 1 to 10 using while, for loop, and loop is given below. The given program is compiled and executed successfully.

// Rust program to print numbers from 
// 1 to 10 using while, for loop, and loop

fn main() {
    let mut cnt=0;
    
    cnt=1;
    println!("Number 1 to 10 using while loop");
    while(cnt<=10)
    {
        print!("{} ",cnt);
        cnt=cnt+1;
    }
    
    println!("\nNumber 1 to 10 using for loop");
    for cnt in 1..11
    {
        print!("{} ",cnt);
    }
    
    println!("\nNumber 1 to 10 using loop");
    cnt=1;
    loop
    {
        print!("{} ",cnt);
        cnt=cnt+1;
        if(cnt>10)
        {
            break;
        }
    }
}

Output:

Number 1 to 10 using while loop
1 2 3 4 5 6 7 8 9 10 
Number 1 to 10 using for loop
1 2 3 4 5 6 7 8 9 10 
Number 1 to 10 using loop
1 2 3 4 5 6 7 8 9 10

Explanation:

Here, we created  a variable cnt with initial value 0. Then we used while loop, for loop, and loop to print numbers from 1 to 10.

Rust Looping Programs »





Comments and Discussions!

Load comments ↻





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