Menu
Coddy logo textTech

Recap - Sigma Function

Part of the Fundamentals section of Coddy's Java journey — lesson 55 of 73.

challenge icon

Challenge

Easy

Write 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