GitHub

Practice

Practice - 026

A program to explain loop. Find sum of even numbers below a number 'n' (do-while pattern)

use text_io::scan;
fn main() {
practice26()
}
fn practice26() {
let n: u32;
let mut i: u32 = 0;
let mut sum: u32 = 0;
println!("Enter the value of n:");
scan!("{}", n);
loop {
sum = sum + i;
i = i + 2;
if i > n {
break;
}
}
println!("Sum of even numbers below {} is {}", n, sum);
}

Config

[package]
name = "practice"
version = "0.1.0"
edition = "2021"
[dependencies]
text_io = "0.1.12"

Result

Enter the value of n:
4
Sum of even numbers below 4 is 6
Previous
Practice - 025