{"id":"for-loops-repeating-calculation","slug":"repeating-a-calculation","title":"Repeating a calculation","text":"The loop variable can take part in the calculation. Here the loop adds up 1 + 2 + 3 + ... up to a number from the input:\n\n```java\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int last = Integer.valueOf(scanner.nextLine());\n        int sum = 0;\n        for (int i = 1; i <= last; i++) {\n            sum += i;\n        }\n        System.out.println(\"Sum: \" + sum);\n    }\n}\n```\n\n```input\n4\n```\n\n```output\nSum: 10\n```\n\nTraced, the sum goes 0, 1, 3, 6, 10. The pattern has the same pieces as summing input: a sum that starts at 0 before the loop, and one addition per round. Only now the added value is `i` itself, or anything calculated from it.\n","task":"Read two positive numbers and multiply them without `*`: add the first number to a sum as many times as the second number says. For the input `6` and `4` the program prints:\n\n```\n24\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int number = Integer.valueOf(scanner.nextLine());\n        int times = Integer.valueOf(scanner.nextLine());\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int number = Integer.valueOf(scanner.nextLine());\n        int times = Integer.valueOf(scanner.nextLine());\n        int result = 0;\n        for (int i = 0; i < times; i++) {\n            result += number;\n        }\n        System.out.println(result);\n    }\n}\n","hints":["A loop that runs `times` rounds, with a sum that starts at 0.","Each round: `result += number;`"],"tests":[{"name":"Test 1","stdin":"6\n4\n","expect":"24","hidden":false},{"name":"Test 2","stdin":"9\n1\n","expect":"9","hidden":true},{"name":"Test 3","stdin":"12\n12\n","expect":"144","hidden":true}],"require":[],"forbid":[{"pattern":"\\*","message":"Multiply with a loop of additions, without `*`."}],"more":[{"task":"Read a number `n` and print the sum of the squares from 1 to `n`: 1 times 1, plus 2 times 2, and so on. For the input `3` the program prints:\n\n```\n14\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int n = Integer.valueOf(scanner.nextLine());\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int n = Integer.valueOf(scanner.nextLine());\n        int sum = 0;\n        for (int i = 1; i <= n; i++) {\n            sum += i * i;\n        }\n        System.out.println(sum);\n    }\n}\n","hints":["The same loop as the example, but each round adds `i * i`.","For 3 that's 1 + 4 + 9."],"tests":[{"name":"Test 1","stdin":"3\n","expect":"14","hidden":false},{"name":"Test 2","stdin":"1\n","expect":"1","hidden":true},{"name":"Test 3","stdin":"10\n","expect":"385","hidden":true}],"require":[],"forbid":[]},{"task":"Read a start and an end, and print the sum of the odd numbers from the start to the end. For the input `3` and `9` the program prints:\n\n```\nSum of odd numbers: 24\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int start = Integer.valueOf(scanner.nextLine());\n        int end = Integer.valueOf(scanner.nextLine());\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int start = Integer.valueOf(scanner.nextLine());\n        int end = Integer.valueOf(scanner.nextLine());\n        int sum = 0;\n        for (int i = start; i <= end; i++) {\n            if (i % 2 != 0) {\n                sum += i;\n            }\n        }\n        System.out.println(\"Sum of odd numbers: \" + sum);\n    }\n}\n","hints":["Go through every number from the start to the end, and add it only when it's odd.","A number is odd when `i % 2 != 0` (this also works for negative numbers, where `% 2` gives -1)."],"tests":[{"name":"Test 1","stdin":"3\n9\n","expect":"Sum of odd numbers: 24","hidden":false},{"name":"Test 2","stdin":"4\n4\n","expect":"Sum of odd numbers: 0","hidden":true},{"name":"Test 3","stdin":"-5\n2\n","expect":"Sum of odd numbers: -8","hidden":true}],"require":[],"forbid":[]}]}