Print Right-angled Triangle Star Pattern in C and Java
Write a C and Java program to print a Right-angled Triangle pattern formed by the star (*) character.
This post covers the following patterns formed by inverting the Right-angled Triangle and its mirror:
Pattern 1: Inverted Right-angled Triangle
Pattern 2: Mirror of Inverted Right-angled Triangle
Pattern 3: Hollow and Inverted Right-angled Triangle
Pattern 4: Mirror of Hollow and Inverted Right-angled Triangle
Pattern 1: Inverted Right-angled Triangle
* * * * *
* * * *
* * *
* *
*
If we have n rows, the first row will display n stars; the 2nd row will display n-1 stars; the 3rd row will display n-2 stars, and so on. We can use two nested loops to print this pattern, where the outer loop represents row number (say i) and the inner loop prints the star pattern (n-i+1 times).
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> int main(void) { // `n` is the total number of rows int n = 5; int i, j; // do for each row for (i = n; i >= 1; i--) { // print '*' i times for (j = 1; j <= i; j++) { printf("*"); } // move to the next line printf("\n"); } return 0; } |
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Main { public static void main(String[] args) { // `n` is the total number of rows final int n = 5; // Do for each row for (int i = n; i >= 1; i--) { // print '*' i times for (int j = 1; j <= i; j++) { System.out.print('*'); } // move to the next line System.out.print(System.lineSeparator()); } } } |
Pattern 2: Mirror of Inverted Right-angled Triangle
* * * * *
* * * *
* * *
* *
*
Suppose we have n rows. The first row will contain 0 spaces, followed by n stars, the 2nd row will contain 1 space, followed by n-1 stars, the 3rd row will contain 2 spaces, followed by n-2 stars, and so on. We can use nested loops to print this pattern, where the outer loop represents row number (say i) and the inner loop prints space (i-1 times), followed by the star pattern (n-i+1 times).
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> int main(void) { // `n` is the total number of rows int n = 5; int i, j, k; for (i = 1; i <= n; i++) { // loop for printing space for (j = 1; j < i; j++) { printf(" "); } // loop for printing '*' for (k = i; k <= n; k++) { printf("*"); } // move to the next line printf("\n"); } return 0; } |
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Main { public static void main(String[] args) { // `n` is the total number of rows final int n = 5; for (int i = 1; i <= n; i++) { // loop for printing space for (int j = 1; j < i; j++) { System.out.print(' '); } // loop for printing '*' for (int k = i; k <= n; k++) { System.out.print('*'); } // move to the next line System.out.print(System.lineSeparator()); } } } |
Pattern 3: Hollow and Inverted Right-angled Triangle
* * * * *
* *
* *
* *
*
The idea remains the same as Pattern 1, but here we print ‘*’ only for the first row and first & last positions for the remaining rows. We fill space for all other positions.
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <stdio.h> int main(void) { // `n` is the total number of rows int n = 5; int i, j; // do for each row for (i = n; i >= 1; i--) { // print '*' i times for (j = 1; j <= i; j++) { // print '*' for boundaries if (i == n || j == 1 || j == i) { printf("*"); } else { printf(" "); } } // move to the next line printf("\n"); } return 0; } |
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class Main { public static void main(String[] args) { // `n` is the total number of rows final int n = 5; // Do for each row for (int i = n; i >= 1; i--) { // print '*' i times for (int j = 1; j <= i; j++) { { // print '*' for boundaries if (i == n || j == 1 || j == i) { System.out.print('*'); } else { System.out.print(' '); } } } // move to the next line System.out.print(System.lineSeparator()); } } } |
Pattern 4: Mirror of Hollow and Inverted Right-angled Triangle
* * * * *
* *
* *
* *
*
Here the idea remains the same as Pattern 2, but we print ‘*’ only for the first row and the first & last cell of the remaining rows. Spaces will fill all other positions.
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <stdio.h> int main(void) { // `n` is the total number of rows int n = 5; int i, j, k; for (i = 1; i <= n; i++) { // loop for printing space for (j = 1; j < i; j++) { printf(" "); } // loop for printing '*' for (k = i; k <= n; k++) { // print '*' for boundaries if (i == 1 || k == i || k == n) { printf("*"); } else { printf(" "); } } // move to the next line printf("\n"); } return 0; } |
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Main { public static void main(String[] args) { // `n` is the total number of rows final int n = 5; for (int i = 1; i <= n; i++) { // loop for printing space for (int j = 1; j < i; j++) { System.out.print(' '); } // loop for printing '*' for (int k = i; k <= n; k++) { // print '*' for boundaries if (i == 1 || k == i || k == n) { System.out.print('*'); } else { System.out.print(' '); } } // move to the next line System.out.print(System.lineSeparator()); } } } |
That’s all about printing a right-angled triangle star pattern in C and Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)