Rust Program to Print Numbers From 1 to 10 Using While, for Loop, and Loop

You are currently viewing Rust Program to Print Numbers From 1 to 10 Using While, for Loop, and Loop

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

// 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

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply