Number and alphabet pattern programs are the natural next step after star patterns. They look different on the screen, but the idea underneath is identical: an outer loop chooses the row, and an inner loop prints the items of that row. In this guide you will learn 15 number pattern programs in Python, C and Java, covering number triangles, Floyd’s triangle, and capital and small alphabet patterns. Every pattern comes with its output, the logic, and a program that was run and tested. A free interactive tool at the top lets you watch the nested loops work, one step at a time.
Watch how the outer loop picks the row and the inner loop prints each number or letter.
Graphical view
Code
Output
Complete output
What You Will Learn
- How pattern programs work (the 3-question method)
- How letters are printed from numbers (ASCII)
- 5 number pattern programs
- 5 capital alphabet pattern programs
- 5 small alphabet pattern programs
- Quick formula table, common mistakes and practice questions
- Frequently asked questions
How Pattern Programs Work
A nested for loop is a loop placed inside another loop. The outer loop controls the rows. For every single row, the inner loop runs from start to finish and prints the items of that row. After the inner loop ends, we print a new line and the outer loop moves to the next row.
To solve any pattern, ask yourself three questions before writing code:
| Question | Example: Number Pyramid |
|---|---|
| How many rows are there? | n rows |
| How many spaces and items are in row i? | n - i spaces and 2*i - 1 items |
| What is printed at position j of the row? | j if j ≤ i, otherwise 2*i - j |
Once you can answer these three questions, the program almost writes itself. Every program on this page follows the same skeleton: an outer loop for i, an optional loop for spaces, an inner loop for j, and a new line at the end of each row.
How Letters Are Printed from Numbers (ASCII)
Computers store every letter as a number called its ASCII value. Capital letters start at 65 and small letters start at 97. That means we can build a letter by adding a position number to 64 (capital) or 96 (small).
| Letter | ASCII value | Formula |
|---|---|---|
| A | 65 | 64 + 1 |
| B | 66 | 64 + 2 |
| Z | 90 | 64 + 26 |
| a | 97 | 96 + 1 |
| z | 122 | 96 + 26 |
To turn that number into a character, Python uses chr(), C uses the %c format in printf, and Java uses a (char) cast. You will see all three in the programs below.
Number Pattern Programs
These five patterns print digits. Start with the first one, because the next four are small changes to it.
1. Number Triangle (1, 12, 123…)
Row i prints the numbers from 1 up to i. The inner loop counter is already the number we want, so no extra variable is needed. This is the best first number pattern to learn.
Output (n = 5):
1 12 123 1234 12345
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasiitems, no leading spaces. - Value printed: the inner loop counter
jitself.
Python program
n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end="")
print()
C program
#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
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(j);
}
System.out.println();
}
}
}
Dry run for n = 3:
| Row (i) | Spaces | Items | Printed in this row |
|---|---|---|---|
| 1 | 0 | 1 | 1 |
| 2 | 0 | 2 | 12 |
| 3 | 0 | 3 | 123 |
2. Repeated Number Triangle (1, 22, 333…)
Every item in row i is the row number itself. The inner loop only decides how many times that number is printed.
Output (n = 5):
1 22 333 4444 55555
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasiitems, no leading spaces. - Value printed: the row number
i, repeated.
Python program
n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
print(i, end="")
print()
C program
#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d", i);
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
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);
}
System.out.println();
}
}
}
3. Inverted Number Triangle (12345, 1234, 123…)
The first row is the longest and each next row loses one number. Row i has n - i + 1 items, still counted from 1.
Output (n = 5):
12345 1234 123 12 1
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasn - i + 1items, no leading spaces. - Value printed: the inner loop counter
jitself.
Python program
n = 5
for i in range(1, n + 1):
for j in range(1, n-i+1 + 1):
print(j, end="")
print()
C program
#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i+1; j++) {
printf("%d", j);
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i+1; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
4. Number Pyramid (1, 121, 12321…)
Each row is centred with n - i spaces, and then prints 2*i - 1 numbers. The numbers rise to i in the middle and fall back to 1, so every row reads the same from both sides.
Output (n = 5):
1 121 12321 1234321 123454321
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasn - ispaces first, then2*i - 1items. - Value printed:
jwhen j ≤ i, otherwise2*i - j(this makes the numbers climb up and come back down).
Python program
n = 5
for i in range(1, n + 1):
for j in range(1, n-i + 1):
print(" ", end="")
for j in range(1, 2*i-1 + 1):
print(j if j <= i else 2*i - j, end="")
print()
C program
#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i; j++) {
printf(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
printf("%d", (j <= i ? j : 2*i - j));
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
System.out.print((j <= i ? j : 2*i - j));
}
System.out.println();
}
}
}
Dry run for n = 3:
| Row (i) | Spaces | Items | Printed in this row |
|---|---|---|---|
| 1 | 2 | 1 | 1 |
| 2 | 1 | 3 | 121 |
| 3 | 0 | 5 | 12321 |
5. Floyd’s Triangle (1, 2 3, 4 5 6…)
Unlike the other patterns, the numbers do not restart in each row. One counter keeps increasing across the whole program. Create it before the outer loop; if you create it inside the loop, every row will start from 1 again.
Output (n = 5):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasiitems, no leading spaces. - Value printed: a running counter
numthat is printed and then increased by 1 (it is never reset).
Python program
n = 5
num = 1
for i in range(1, n + 1):
for j in range(1, i + 1):
print(num, end=" ")
num += 1
print()
C program
#include <stdio.h>
int main() {
int n = 5;
int num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", num);
num++;
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
public static void main(String[] args) {
int n = 5;
int num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}
Dry run for n = 3:
| Row (i) | Spaces | Items | Printed in this row |
|---|---|---|---|
| 1 | 0 | 1 | 1 |
| 2 | 0 | 2 | 2 3 |
| 3 | 0 | 3 | 4 5 6 |
Capital Alphabet Pattern Programs
These patterns use the same shapes as the number patterns. The only new step is converting the number into a capital letter with 64 + position.
1. Capital Alphabet Triangle (A, AB, ABC…)
Letters come from numbers through ASCII. Since A is 65, the expression 64 + j gives A for j = 1, B for j = 2, and so on. The structure is exactly the same as the number triangle.
Output (n = 5):
A AB ABC ABCD ABCDE
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasiitems, no leading spaces. - Value printed: the j-th letter, found with
64 + j.
Python program
n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
print(chr(64 + j), end="")
print()
C program
#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%c", 64 + j);
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
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((char)(64 + j));
}
System.out.println();
}
}
}
Dry run for n = 3:
| Row (i) | Spaces | Items | Printed in this row |
|---|---|---|---|
| 1 | 0 | 1 | A |
| 2 | 0 | 2 | AB |
| 3 | 0 | 3 | ABC |
2. Repeated Capital Triangle (A, BB, CCC…)
Row i prints the i-th letter, i times. Only the value changes compared with the number version.
Output (n = 5):
A BB CCC DDDD EEEEE
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasiitems, no leading spaces. - Value printed: the i-th letter, found with
64 + i, repeated.
Python program
n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
print(chr(64 + i), end="")
print()
C program
#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%c", 64 + i);
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
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((char)(64 + i));
}
System.out.println();
}
}
}
3. Inverted Capital Triangle (ABCDE, ABCD, ABC…)
The row length shrinks from n to 1, but every row still starts from A.
Output (n = 5):
ABCDE ABCD ABC AB A
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasn - i + 1items, no leading spaces. - Value printed: the j-th letter, found with
64 + j.
Python program
n = 5
for i in range(1, n + 1):
for j in range(1, n-i+1 + 1):
print(chr(64 + j), end="")
print()
C program
#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i+1; j++) {
printf("%c", 64 + j);
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i+1; j++) {
System.out.print((char)(64 + j));
}
System.out.println();
}
}
}
4. Capital Alphabet Pyramid (A, ABA, ABCBA…)
This is the number pyramid with each number converted into a letter. The middle letter is the i-th letter, and both sides mirror each other.
Output (n = 5):
A ABA ABCBA ABCDCBA ABCDEDCBA
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasn - ispaces first, then2*i - 1items. - Value printed: the letter for
jwhen j ≤ i, otherwise for2*i - j.
Python program
n = 5
for i in range(1, n + 1):
for j in range(1, n-i + 1):
print(" ", end="")
for j in range(1, 2*i-1 + 1):
print(chr(64 + (j if j <= i else 2*i - j)), end="")
print()
C program
#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i; j++) {
printf(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
printf("%c", 64 + (j <= i ? j : 2*i - j));
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
System.out.print((char)(64 + (j <= i ? j : 2*i - j)));
}
System.out.println();
}
}
}
Dry run for n = 3:
| Row (i) | Spaces | Items | Printed in this row |
|---|---|---|---|
| 1 | 2 | 1 | A |
| 2 | 1 | 3 | ABA |
| 3 | 0 | 5 | ABCBA |
5. Continuous Capital Triangle (A, B C, D E F…)
This is Floyd’s triangle with letters. Because the alphabet has only 26 letters, the program uses ch % 26 so that it wraps back to A after Z.
Output (n = 5):
A B C D E F G H I J K L M N O
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasiitems, no leading spaces. - Value printed: a running counter
ch, converted with65 + ch % 26, so after the last letter it wraps around.
Python program
n = 5
ch = 0
for i in range(1, n + 1):
for j in range(1, i + 1):
print(chr(65 + ch % 26), end=" ")
ch += 1
print()
C program
#include <stdio.h>
int main() {
int n = 5;
int ch = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%c ", 65 + ch % 26);
ch++;
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
public static void main(String[] args) {
int n = 5;
int ch = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print((char)(65 + ch % 26) + " ");
ch++;
}
System.out.println();
}
}
}
Small Alphabet Pattern Programs
Small letter patterns need just one change from the capital ones: replace 64 with 96 (and 65 with 97). Everything else stays the same.
| Capital version | Small version |
|---|---|
chr(64 + j) | chr(96 + j) |
chr(65 + ch % 26) | chr(97 + ch % 26) |
1. Small Alphabet Triangle (a, ab, abc…)
The only difference from the capital version is the starting value: small letters begin at 97, so the expression becomes 96 + j and j = 1 gives a.
Output (n = 5):
a ab abc abcd abcde
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasiitems, no leading spaces. - Value printed: the j-th letter, found with
96 + j.
Python program
n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
print(chr(96 + j), end="")
print()
C program
#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%c", 96 + j);
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
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((char)(96 + j));
}
System.out.println();
}
}
}
2. Repeated Small Triangle (a, bb, ccc…)
Row i prints the i-th small letter, i times, using 96 + i.
Output (n = 5):
a bb ccc dddd eeeee
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasiitems, no leading spaces. - Value printed: the i-th letter, found with
96 + i, repeated.
Python program
n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
print(chr(96 + i), end="")
print()
C program
#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%c", 96 + i);
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
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((char)(96 + i));
}
System.out.println();
}
}
}
3. Inverted Small Triangle (abcde, abcd, abc…)
Same shape as before, only the letters are small: every row starts from a and the row length shrinks from n to 1.
Output (n = 5):
abcde abcd abc ab a
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasn - i + 1items, no leading spaces. - Value printed: the j-th letter, found with
96 + j.
Python program
n = 5
for i in range(1, n + 1):
for j in range(1, n-i+1 + 1):
print(chr(96 + j), end="")
print()
C program
#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i+1; j++) {
printf("%c", 96 + j);
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i+1; j++) {
System.out.print((char)(96 + j));
}
System.out.println();
}
}
}
4. Small Alphabet Pyramid (a, aba, abcba…)
Same mirror idea as the capital pyramid. The offset changes from 64 to 96, nothing else.
Output (n = 5):
a aba abcba abcdcba abcdedcba
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasn - ispaces first, then2*i - 1items. - Value printed: the letter for
jwhen j ≤ i, otherwise for2*i - j.
Python program
n = 5
for i in range(1, n + 1):
for j in range(1, n-i + 1):
print(" ", end="")
for j in range(1, 2*i-1 + 1):
print(chr(96 + (j if j <= i else 2*i - j)), end="")
print()
C program
#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i; j++) {
printf(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
printf("%c", 96 + (j <= i ? j : 2*i - j));
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
System.out.print((char)(96 + (j <= i ? j : 2*i - j)));
}
System.out.println();
}
}
}
5. Continuous Small Triangle (a, b c, d e f…)
The same running counter idea, now with small letters. After z it wraps back to a through ch % 26.
Output (n = 5):
a b c d e f g h i j k l m n o
Logic:
- Outer loop: runs
ntimes, one pass per row. - Row
ihasiitems, no leading spaces. - Value printed: a running counter
ch, converted with97 + ch % 26, so after the last letter it wraps around.
Python program
n = 5
ch = 0
for i in range(1, n + 1):
for j in range(1, i + 1):
print(chr(97 + ch % 26), end=" ")
ch += 1
print()
C program
#include <stdio.h>
int main() {
int n = 5;
int ch = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%c ", 97 + ch % 26);
ch++;
}
printf("\n");
}
return 0;
}
Java program
public class Pattern {
public static void main(String[] args) {
int n = 5;
int ch = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print((char)(97 + ch % 26) + " ");
ch++;
}
System.out.println();
}
}
}
Quick Formula Table
Use this table to revise before an exam or a practical. For small letters, replace 64 with 96 and 65 with 97.
| Pattern | Spaces in row i | Items in row i | Value printed |
|---|---|---|---|
| Number Triangle (1, 12, 123…) | none | i | j |
| Repeated Number Triangle (1, 22, 333…) | none | i | i |
| Inverted Number Triangle (12345, 1234, 123…) | none | n – i + 1 | j |
| Number Pyramid (1, 121, 12321…) | n – i | 2*i – 1 | j or 2*i – j |
| Floyd’s Triangle (1, 2 3, 4 5 6…) | none | i | counter |
| Capital Alphabet Triangle (A, AB, ABC…) | none | i | 64 + j |
| Repeated Capital Triangle (A, BB, CCC…) | none | i | 64 + i |
| Inverted Capital Triangle (ABCDE, ABCD, ABC…) | none | n – i + 1 | 64 + j |
| Capital Alphabet Pyramid (A, ABA, ABCBA…) | n – i | 2*i – 1 | 64 + (j or 2*i – j) |
| Continuous Capital Triangle (A, B C, D E F…) | none | i | 65 + ch % 26 |
Common Mistakes
- Starting j from 0 when j is the value. Number patterns print j, so the inner loop must start at 1, otherwise you will see a 0 or the wrong letter.
- Forgetting the new line. Without
print()(orprintf("\n")) after the inner loop, the whole pattern comes out on one line. - Off-by-one ASCII offset. Use 64 + j (not 65 + j) when j starts at 1. With 65 + j you will get B instead of A.
- Resetting the counter inside the outer loop. In Floyd’s triangle the counter must be created before the outer loop, or every row restarts.
- Printing a number instead of a letter.
print(64 + j)prints 65. You must convert it withchr()in Python,%cin C, or a(char)cast in Java.
Practice Questions
- Print a right-aligned number triangle (spaces first, then 1, 12, 123…). Hint: reuse the spaces loop from the pyramid.
- Print the pattern E, ED, EDC, EDCB, EDCBA. Hint: the letter goes down, so try 70 – j.
- Print a number diamond by joining the number pyramid with an inverted version of it.
- Print the pattern 1, 01, 101, 0101 (alternating 1 and 0). Hint: print 1 when i – j is even, otherwise 0.
- Print a hollow number pyramid. Hint: print the digit only on the first and last position of each row.
- Change the continuous alphabet triangle so that it stops at Z instead of wrapping around.
Frequently Asked Questions
What is a pattern program in programming?
A pattern program prints numbers, letters or symbols in a fixed shape such as a triangle, pyramid or diamond. It is used to practise loops and logical thinking.
Why do pattern programs need nested loops?
A pattern has rows and columns. One loop is needed to move through the rows and another to move through the items in each row, so two loops are placed one inside the other.
How do I print alphabets in a pattern in Python?
Calculate the ASCII number of the letter and convert it with chr(). For example, chr(64 + 1) gives A and chr(96 + 1) gives a.
What is the difference between capital and small alphabet patterns?
Only the starting ASCII value changes. Capital letters begin at 65 (A) and small letters begin at 97 (a), so the offset is 64 for capital letters and 96 for small letters.
What is Floyd’s triangle?
Floyd’s triangle is a right-angled triangle of consecutive numbers, where row 1 has one number, row 2 has two numbers, and so on. The counter continues from one row to the next without restarting.
What is the time complexity of a pattern program?
For a triangle, the total number of printed items is about n(n + 1) / 2, so the time complexity is O(n²).
Can I use a while loop instead of a for loop?
Yes. The logic is the same. You only need to create the counter before the loop and increase it manually inside the loop.
Which language is best for learning pattern programs?
Python has the shortest syntax, so it is the easiest way to start. C and Java are useful if your syllabus or practicals require them. The logic of the pattern is the same in all three.