package eecs1022.finala; /** * This question is worth 5 marks. Partial marks will be awarded for this question. * * @author Franck van Breugel */ public class Question2 { /** * Returns the value of 1 + 2 + ... + n. You have to use a loop to solve this question. * You may assume that n is greater than 0. * * @param n a positive integer. * @return the value of 1 + 2 + ... + n */ public static int sum(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum = sum + i; } return sum; } /** * Tests method. You may add more test cases. * * @param args not applicable. */ public static void main(String[] args) { System.out.println(Question2.sum(1)); System.out.println(Question2.sum(2)); System.out.println(Question2.sum(3)); System.out.println(Question2.sum(4)); System.out.println(Question2.sum(5)); } }