Skip to content
← All examples

Sum of evens

Rust Loops

for over the range 1..=n

Flowchart (ISO 5807)

YesNoStartInput nsum = 0i = 1, n, 1i % 2 == 0sum += iReturn sumEndFigure 1 — sum_even

Source code

fn sum_even(n: i32) -> i32 {
    let mut sum = 0;
    for i in 1..=n {
        if i % 2 == 0 {
            sum += i;
        }
    }
    sum
}