
Day 5 of 30 Days of Code asks you to print the multiplication table of a number, from 1 to 10. Yes, the same table you recited in primary school. The goal is to get you writing your first real loop: a block of code that runs again and again with a value that changes each time, instead of ten copy-pasted print statements.
Loops are where programming starts to save you real effort. Ten lines by hand is annoying, a thousand is impossible, and with a loop both are the same three lines of code. This problem also quietly tests output formatting, because the judge expects the spaces and the equals sign in exactly the right places.
Problem
Read one integer n. Print its first ten multiples, one per line, in the form n x i = result, where i goes from 1 to 10 and result is n times i.
Input format: a single integer n.
Output format: exactly 10 lines. Line i looks like n x i = n*i, with a single space on each side of x and =. The x is a lowercase letter, not a multiplication symbol.
Constraints: 2 ≤ n ≤ 20.
Input:
2
Output:
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Approach
Look at the output and ask what changes from line to line. Only two things do: the multiplier i and the result. The number n, the letter x and the equals sign stay the same. So you need one variable that counts from 1 to 10, and on every step you print a line built from n, i and n * i.
A for loop fits this perfectly, because you know in advance how many times it should run. It has three parts in its header:
- Start:
int i = 1creates the counter. - Condition:
i <= 10is checked before every round. When it becomes false, the loop stops. - Update:
i++adds 1 to the counter after each round.
Dry run with n = 2: round 1 has i = 1, prints 2 x 1 = 2, then i becomes 2. Round 2 prints 2 x 2 = 4. This continues until round 10 prints 2 x 10 = 20. Now i becomes 11, the condition 11 <= 10 is false, and the loop ends. Exactly ten lines were printed.
Solution in Java
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.close();
// i goes 1, 2, ..., 10 : one line per multiple
for (int i = 1; i <= 10; i++) {
System.out.println(n + " x " + i + " = " + (n * i));
}
}
}
How the print line works: in Java, + between a String and a number joins them into a longer string. So n + " x " + i with n = 2 and i = 3 becomes "2 x 3". The spaces live inside the quoted pieces, which is why the output has them.
The brackets around (n * i) are worth a moment. Multiplication happens before addition, so the code is correct even without them. But if you ever change it to n + i without brackets, Java would join the two numbers as text and print something like 23 instead of 5. Brackets make your intent clear and protect you from that surprise.
The solution earlier on this page was already correct. I tidied it and tested it with the sample (n = 2) and with both limits. With n = 20 the last line is 20 x 10 = 200, and the largest possible result (200) fits easily in an int.
Output
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Complexity
The loop always runs 10 times, so time is O(1) for this problem. If the table length were a variable k, it would be O(k). Space is O(1).
Common mistakes
- Starting the loop at 0 (
int i = 0). You get an extra line2 x 0 = 0and the judge rejects it. - Using
i < 10instead ofi <= 10, which stops at 9 and misses the last line. - Missing or extra spaces, for example
2x1=2. Copy the spacing from the sample exactly. - Using
printinstead ofprintln, which puts all ten results on one long line.
The Java track has the same exercise as Java Loops I, where we solve it with printf. Next in this series: Day 6: Let's Review.
Tip: try to solve the problem yourself before reading the solution. Use the code as a reference to check your approach, and make sure you understand why it works.
More HackerRank solutions All coding problems MCQ practice
See everything in one place: Coding Problems and Solutions
Comments
Post a Comment