New: 213 pattern programs with tested code in C, C++, Java and Python Explore patterns

Number and Symbol Fill Pattern (1****, 22***)

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
  1. Number and Symbol Fill Pattern output
  2. How the number and symbol fill pattern works
  3. Number and Symbol Fill Pattern in C
  4. Number and Symbol Fill Pattern in C++
  5. Number and Symbol Fill Pattern in Java
  6. Number and Symbol Fill Pattern in Python
  7. Time and space complexity
  8. Frequently asked questions

Number and Symbol Fill Pattern output

Output (n = 5):

1****
22***
333**
4444*
55555
Number and Symbol Fill Pattern layout diagram, row by row (n = 5)
How each row of the number and symbol fill pattern is built (n = 5). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Print i numbers (value i).
  3. Print n βˆ’ i stars.
  4. Move to the next line.

Key takeaways

  • Each row has i digits plus n βˆ’ i stars: always n characters.
  • 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.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 9 of 17 in Symbol Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.