Q:

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

belongs to collection: Rust Looping Programs

0

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

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 print numbers from 1 to 10 using whilefor 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.

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

total answers (1)

Rust program to implement infinite loop using whil... >>