The number and symbol fill pattern prints a square in which the row number grows from the left and stars fill the rest of the row: 1****, 22***, 333**, 4444*, 55555. It is a common ICSE pattern question.
Quick answer: Every row is n characters wide. Row i prints the digit i, i times, and then n β i stars. The digits form a triangle and the stars fill the triangle above it, so together they make a full square.
This program is part of the Symbol Patterns collection.
Table of contents8 sections
Number and Symbol Fill Pattern output
Output (n = 5):
1**** 22*** 333** 4444* 55555

Same shape, other characters: the Cyclic Number Square and Number Square programs draw this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.
How the number and symbol fill pattern works
Two triangles share one square here. The digits are the repeated number triangle (1, 22, 333), and the stars are an inverted right triangle that fits exactly into the space left over. Because the two counts, i and n β i, always add up to n, every row has the same width.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
inumbers (valuei). - Print
n β istars. - Move to the next line.
Key takeaways
- Each row has
idigits plusn β istars: alwaysncharacters. - The digits make a triangle; the stars fill the rest of the square.
- The diagonal where they meet moves one column right per row.
- Use any symbol in place of the star, such as # or $.
Number and Symbol Fill Pattern in C
#include <stdio.h>
int main(void) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
printf("%d", i);
for (int j = 1; j <= n - i; j++)
printf("*");
printf("\n");
}
return 0;
}Number and Symbol Fill Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
cout << i;
for (int j = 1; j <= n - i; j++)
cout << "*";
cout << endl;
}
return 0;
}Number and Symbol Fill Pattern in Java
public class NumberSymbolFill {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
System.out.print(i);
for (int j = 1; j <= n - i; j++)
System.out.print("*");
System.out.println();
}
}
}Number and Symbol Fill Pattern in Python
n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
print(i, end="")
for j in range(1, n - i + 1):
print("*", end="")
print()Time and space complexity
The program prints one character for each position in the pattern, so for n rows it runs in O(nΒ²) time. It only needs a few loop variables, so the extra memory is O(1).
Frequently asked questions
How do I put the stars first (****1)?
Print the n β i stars before the digits. The digits then line up on the right.
How do I count down instead (5****, 44***)?
Print n β i + 1 as the digit while keeping the same counts.
How do I let the user choose n?
Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.






