Challenge: Number Staircase
Completed
Read n and print a staircase of numbers: row r prints the numbers 1 to r, separated by spaces.
Input
One integer n (1 ≤ n ≤ 9).
Output
n lines.
Example
Input
4
Output
1 1 2 1 2 3 1 2 3 4
Editor
C
Hints
Use one loop for the rows and one inside it for the numbers.
The inner loop goes from 1 to row.
Solution explanation
The outer loop picks the row; the inner loop prints that row's numbers, with a space before every number except the first.
Show solution
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for (int row = 1; row <= n; row++) {
for (int i = 1; i <= row; i++) {
if (i > 1) {
printf(" ");
}
printf("%d", i);
}
printf("\n");
}
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module