Recap - Sigma Function
Part of the Fundamentals section of Coddy's Rust journey — lesson 53 of 75.
Challenge
EasyWrite a function named sigma with one argument that represents a number n.
The function will return the sum of all the numbers from 1 to n (including).
For example, for sigma(5), the function will return 15, because 15 = 1 + 2 + 3 + 4 + 5.
Try it yourself
use std::io;
fn sigma(n: i32) -> i32 {
// Write your code below
}
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let n: i32 = input.trim().parse().unwrap();
let res = sigma(n);
println!("{}", res);
}All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 311Functions
Declaring FunctionsParameters and ArgumentsReturn ValuesMultiple Return ValuesRecap - Sigma FunctionRecap - Validation Function3Variables Part 2
Type DeclarationNaming ConventionsType InferenceRecap - Initialize VariablesType Casting9Loops
For Over SeriesWhile LoopBreakContinueNested LoopLoop LabelsInfinite LoopRecap - Dynamic Input