Table of Contents
Introduction
The program here illustrates Java Program to print Reverse Pyramid Star Pattern.
Approach
- 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.
- Based on the information above, we will be running two loops.
- 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.
- The inner loop will run from 1 to (outer loop control variable – 1)/
- 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