{"id":"for-loops-multiplying","slug":"multiplying","title":"Multiplying in a loop","text":"A product is built up like a sum, with one difference: it starts at **1**, not 0. Starting at 0 would make every product 0.\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 doublings = Integer.valueOf(scanner.nextLine());\n        int value = 1;\n        for (int i = 0; i < doublings; i++) {\n            value *= 2;\n        }\n        System.out.println(\"1 doubled \" + doublings + \" times is \" + value);\n    }\n}\n```\n\n```input\n10\n```\n\n```output\n1 doubled 10 times is 1024\n```\n\nWith `0` as the input the loop doesn't run, and the value stays 1, which is right: doubling zero times changes nothing. (The same loop calculates 2 to the power of the input.)\n\nProducts grow fast. An `int` holds values up to 2147483647 (module 4), so these programs only work for fairly small inputs. The tests here stay inside that limit.\n","task":"Read a base and an exponent, and calculate the power with a loop, without `Math.pow`. For the input `3` and `4` the program prints:\n\n```\n3 to the power 4 is 81\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 base = Integer.valueOf(scanner.nextLine());\n        int exponent = 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 base = Integer.valueOf(scanner.nextLine());\n        int exponent = Integer.valueOf(scanner.nextLine());\n        int result = 1;\n        for (int i = 0; i < exponent; i++) {\n            result *= base;\n        }\n        System.out.println(base + \" to the power \" + exponent + \" is \" + result);\n    }\n}\n","hints":["Start the result at 1 and multiply it by the base once per round.","The loop runs `exponent` rounds."],"tests":[{"name":"Test 1","stdin":"3\n4\n","expect":"3 to the power 4 is 81","hidden":false},{"name":"Test 2","stdin":"7\n0\n","expect":"7 to the power 0 is 1","hidden":true},{"name":"Test 3","stdin":"2\n20\n","expect":"2 to the power 20 is 1048576","hidden":true}],"require":[],"forbid":[{"pattern":"Math\\.pow","message":"Calculate the power with a loop, without `Math.pow`."}],"more":[{"task":"In how many different orders can `n` books stand on a shelf? The first place can hold any of the `n` books, the next any of the `n - 1` left, and so on, so the answer is n times (n - 1) times ... times 1. With 0 books there's exactly 1 way (an empty shelf). Read `n` and print the answer. For the input `5` the program prints:\n\n```\n5 books can be arranged in 120 ways\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 books = 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 books = Integer.valueOf(scanner.nextLine());\n        int ways = 1;\n        for (int i = 1; i <= books; i++) {\n            ways *= i;\n        }\n        System.out.println(books + \" books can be arranged in \" + ways + \" ways\");\n    }\n}\n","hints":["Multiply the numbers from 1 to `books` together, starting from 1.","With 0 books the loop doesn't run, and `ways` stays 1, as it should."],"tests":[{"name":"Test 1","stdin":"5\n","expect":"5 books can be arranged in 120 ways","hidden":false},{"name":"Test 2","stdin":"0\n","expect":"0 books can be arranged in 1 ways","hidden":true},{"name":"Test 3","stdin":"10\n","expect":"10 books can be arranged in 3628800 ways","hidden":true}],"require":[],"forbid":[]},{"task":"Read a number `n` and print the product of the odd numbers from 1 to `n`. For the input `7` the program prints `105`, because 1 times 3 times 5 times 7 is 105:\n\n```\n105\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 product = 1;\n        for (int i = 1; i <= n; i += 2) {\n            product *= i;\n        }\n        System.out.println(product);\n    }\n}\n","hints":["The odd numbers are 1, 3, 5 and so on: start at 1 and step by 2.","`for (int i = 1; i <= n; i += 2) { product *= i; }`"],"tests":[{"name":"Test 1","stdin":"7\n","expect":"105","hidden":false},{"name":"Test 2","stdin":"1\n","expect":"1","hidden":true},{"name":"Test 3","stdin":"12\n","expect":"10395","hidden":true}],"require":[],"forbid":[]}]}