Recap - Sigma Function
Part of the Fundamentals section of Coddy's Java journey — lesson 55 of 73.
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
import java.util.Scanner;
public class Main {
public static int sigma(int n) {
// Write your code below
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int res = sigma(n);
System.out.println(res);
}
}All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison10Methods (Functions)
Declaring MethodsMethod ParametersReturn TypesMethod OverloadingRecap - Sigma FunctionRecap - Validation FunctionVoid Methods5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else