Difficulty: Easy
Correct Answer: #include
Explanation:
Introduction / Context:
Printing a multiplication table is a straightforward exercise designed to practise basic loop constructs, formatted output and simple arithmetic in C. The typical task is to read an integer n and print lines showing i × n for i from 1 through 10. This question asks you to identify the code that correctly implements this behaviour and avoids common syntax traps such as stray semicolons in for loops.
Given Data / Assumptions:
Concept / Approach:
A correct solution uses a for loop with a counter i that runs from 1 to 10 inclusive. Inside the loop body, it computes the product i * n and prints a formatted string using printf. The control variable i must be declared and incremented properly, and the loop header must not have an accidental trailing semicolon, which would create an empty loop body and cause incorrect behaviour.
Step-by-Step Solution:
Step 1: Read the value of n with scanf("%d", &n);.
Step 2: Initialise a for loop with int i = 1 and run while i <= 10, incrementing i by one each iteration.
Step 3: Inside the loop, calculate i * n and print it with a message such as printf("%d x %d = %d\", i, n, i * n);.
Step 4: Ensure that there is no semicolon terminating the for statement prematurely.
Step 5: After the loop completes, return 0 from main to indicate successful execution.
Verification / Alternative check:
For n equals 5, the correct program will output lines from "1 x 5 = 5" through "10 x 5 = 50". You can simulate a few iterations to confirm that the loop runs exactly ten times and that the arithmetic expression i * n is correct. Visual inspection confirms that the for loop header is followed immediately by a block containing the printf, without an extra semicolon that would alter the control flow.
Why Other Options Are Wrong:
Option B prints only a fixed string "n x n = n" and does not use a loop or the value read from input properly. Option C contains a stray semicolon at the end of the for header, which causes the loop to iterate with an empty body and then executes the printf once after the loop, with i left at 11. Option D prints the value of n ten times without multiplying by the loop index and does not display the multiplication table format.
Common Pitfalls:
A classic mistake is placing a semicolon after the for statement, which disconnects the loop from the intended body. Another is using incorrect loop bounds, for example starting from zero or stopping at less than 10 when the specification requires 1 through 10. Forgetting to include the line break in printf can also make the output harder to read, though it does not change the numeric correctness.
Final Answer:
The correct code reads n, then uses a for loop from i = 1 to i = 10 and prints each line in the form i x n = i * n with printf.
Discussion & Comments