Java Program to print Reverse Pyramid Star Pattern

by | Jan 18, 2023 | Java, Java Programs

Introduction

The program here illustrates Java Program to print Reverse Pyramid Star Pattern.

Approach

  1. The first step is to take input from the user or otherwise the number of rows of the reverse pyramid pattern you want to print out. This can be done using Scanner Class or Buffered Reader class object.
  2. Based on the information above, we will be running two loops.
  3. The outer loop will iterate through the number of rows of the pattern, that is, from 1 to the number entered into the program via the reader class object.
  4. The inner loop will run from 1 to (outer loop control variable – 1)/
  5. Another inner loop inside it is required that will traverse through row*2 – (outer LCV * 2 – 1)

Example:

Input: number = 7
Output:

*************
 ***********
  *********
   *******
    *****
     ***
      *

Sample Code 1: The Reverse Pyramid Printing program will be implemented using a while loop

//Program that will print the above pattern using a while loop

import java.io.*;

class Demo
{
//Driver function
public static void main (String args [])
{
//number of rows of the pattern that will be printed
int number = 7;
int i = number, j ;

//outer while loop to control the rows

//condition check
while (i > 0)
{
j = 0;
//columnar while loop

while (j ++ < number – i)
{
//whitespace generation
System.out.print (“ “);
}

j = 0;

//while loop
while ( ++ < (i * 2) – 1)
{
// star generation
System.out.print (“ * “);
}

// Printing out a blank line such that execution moves to the next line as well
System.out.println ();

//decrementing the value of i (LCV) to get the desired pattern
i—;
}
}
}

OUTPUT

*************
 ***********
  *********
   *******
    *****
     ***
      *

Sample Code 2: The Reverse Pyramid Printing program will be implemented using a for loop

//Program that will print the above pattern using a for loop

import java.io.*;

class Demo
{
public static void main (String args [])
{
//number of rows of the pattern that will be printed
int number = 7;
int i , j;

//outer for loop that will control the row number
for (i = number; i > = 1; i –)
{
//for loop for space generation
for (j = i; j < number; j ++)
{
System.out.print (“ ”);
}

//for loop to print stars

for (j = 1; j < = (2 * i – 1); j++)
{
System.out.print (“ * ”);
}

// Printing out a line with a space such that execution moves to the next line as well
System.out.println (“ ”);
}
}
}

OUTPUT:

*************
 ***********
  *********
   *******
    *****
     ***
      *

Sample Code 3: The Reverse Pyramid Printing program will be implemented using a do-while loop

//Program that will print the above pattern using a do-while loop

import java.io.*;

public class Demo
{
public static void main (String args [])
{
int number = 7;
int i = number, j;

//do while loop for row number
dp
{
j = 0;

//do while loop for space generation
do
{
//space generation until j ++ < number – i is not true
System.out.print (“ ”);
}

while (j ++ < number – i);
j = 0;

//do a while loop to print stars

do
{
// star generation
System.out.print (“ * ”);
}

while (j ++ < i * 2 – 2);

// whitespace generation
System.out.println (“ ”);
}
// while condition check
while (–i > 0);
}
}

OUTPUT

*************
 ***********
  *********
   *******
    *****
     ***
      *

 

0 Comments

Submit 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.