Program to Print inverted Pyramid Star Pattern
In this article we will are going to see how to print the Reversed Pyramid star pattern in Java.
Example-1 When row values=5 ********* ******* ***** *** *
Example-2 When row value=4 ******* ***** *** *
Let’s see the actual program to print it.
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Approach:
- Enter total row and store it in an integer variable
row. - Take first for loop to print all the rows.
- Take second/inner loop to print spaces.
- Take third/inner loop to print the column values.
- Then go on printing the star symbols according to the iteration.
JAVA Code:
Method-1 : Static Star Character

import java.util.*;
class Main{
public static void main (String[] args)
{
// Height of the pyramid
int row,r, c;
System.out.print("Enter no of rows = ");
Scanner sc= new Scanner(System.in);
row=sc.nextInt();
// Loop for no of ros
for(r=1; r<=row; ++r)
{
// Print spaces
for(c=1; c<=r; ++c)
{
System.out.print(" ");
}
// Print star/
for(c =1; c <=((row*2)-((2*r)-1)); ++c)
{
System.out.print("*");
}
// Print new line
System.out.println("");
}
}
}
Output: Enter no of rows = 5 ********* ******* ***** *** *
Method-2 : User Input Character
import java.util.*;
class Main{
public static void main (String[] args)
{
int row,r, c;
// Height of the pyramid
System.out.print("Enter no of rows : ");
Scanner sc= new Scanner(System.in);
row=sc.nextInt();
System.out.print("Enter symbol : ");
char symbol=sc.next().charAt(0);
// Loop for no of ros
for(r=1; r<=row; ++r)
{
// Print spaces
for(c=1; c<=r; ++c)
{
System.out.print(" ");
}
// Print star/
for(c =1; c <=((row*2)-((2*r)-1)); ++c)
{
System.out.print(symbol);
}
// Print new line
System.out.println("");
}
}
}
Output: Enter no of rows : 5 Enter symbol : @ @@@@@@@@@ @@@@@@@ @@@@@ @@@ @
Explanation:
Let’s understand the program will detailed explanation.
Let we have taken row as 5.
Iteration-I
r=1(passed through first for loop condition) which will execute till r<=row.
Now inner for loop will execute 1 time (print 1 space) because it will execute until c<=r. Another inner loop will execute 10-1 i.e. 9 times which will execute until c<= ((row*2)-((2*r)-1)), here the star will be printed 9 times.
*********
Iteration-II
r=2(passed through first for loop condition) which will execute till r<=row.
Now inner for loop will execute 2 time (print 2 spaces) because it will execute until c<=r. Another inner loop will execute 10-3 i.e. 7 times which will execute until c<= ((row*2)-((2*r)-1)), here the star will be printed 7 times.
*******
Iteration-III
r=3(passed through first for loop condition) which will execute till r<=row.
Now inner for loop will execute 3 time (print 3 spaces) because it will execute until c<=r. Another inner loop will execute 10-5 i.e. 5 times which will execute until c<= ((row*2)-((2*r)-1)), here the star will be printed 5 times.
*****
Iteration-IV
r=4(passed through first for loop condition) which will execute till r<=row.
Now inner for loop will execute 4 time (print 4 spaces) because it will execute until c<=r. Another inner loop will execute 10-7 i.e. 3 times which will execute until c<= ((row*2)-((2*r)-1)), here the star will be printed 3 times.
***
Iteration-V
r=5(passed through first for loop condition) which will execute till r<=row. Now inner for loop will execute 5 time (print 5 spaces) because it will execute until c<=r. Another inner loop will execute 10-9 i.e. 1 times which will execute until c<= ((row*2)-((2*r)-1)), here the star will be printed 1 time.
*
Now r=6 where first for loop condition failed, so further inner for loops won’t be executed. And at last we will see a pattern like this as output.
********* ******* ***** *** *
C Code:
#include <stdio.h>
int main()
{
int r = 0,c = 0;
int row = 0;
printf("Enter no of rows = ");
scanf("%d",&row);
for(r=1; r<=row; ++r)
{
for(c=1; c<=r; ++c)
{
printf(" ");
}
for(c =1; c <=((row*2)-((2*r)-1)); ++c)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output: Enter no of rows = 5 ********* ******* ***** *** *
C++ Code:
#include<iostream>
using namespace std;
int main()
{
int r, c, row;
cout << "Enter no of rows = ";
cin >> row;
for(r=1; r<=row; ++r)
{
for(c=1; c<=r; ++c)
{
cout<<(" ");
}
for(c =1; c <=((row*2)-((2*r)-1)); ++c)
{
cout<<("*");
}
cout<<("\n");
}
return 0;
}
Output: Enter no of rows = 5 ********* ******* ***** *** *
Related Java Star Pattern Programs: