Java Program to Print Inverted Star Pattern
In this tutorial, we will see how to print the inverted star pattern in Java. First, we will ask the user to initialize the number of rows. Then, we will use loops to print the inverted star patterns. But before moving further, if you are not familiar with the concept of the loops in java, then do check the article on Loops in Java.
Input: Enter the number of rows: 4
Output: The pattern is:
********
*****
***
*
This can be done by using the following methods:
Approach 1: Using a For Loop
Approach 2: Using a While loop
Approach 3: Using a do-while loop
Let us look at each of these approaches for a better understanding
Program 1: Print the Inverted Star Pattern
In this program, we will see how to print the inverted star pattern in java using a for loop.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare a variable to store the number of rows.
- Ask the user to initialize the variable.
- Use a for loop to print the pattern.
- To iterate through rows run the outer loop with the structure for(int i=n;i>0;iā).
- To iterate through rows run the inner loops.
- The first inner loop prints space if i>0 && j<n-i.
- The second inner loop prints character if i>0&&j<(i*2)-1
- Display the result.
- Stop
The below example illustrates the implementation of the above algorithm.
//Java Program To Print Inverted Star Pattern
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int n=sc.nextInt();
for(int i=n;i>0 ;i--)
{
for(int j=0;j<n-i;j++)
{
System.out.print(" ");
}
for(int j=0;j<(i*2)-1;j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Enter the number of rows: 7
*************
***********
*********
*******
*****
***
*
Program 2: Print the Inverted Star Pattern
In this program, we will see how to print the inverted star pattern in java using a while loop.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare a variable to store the number of rows.
- Ask the user to initialize the variable.
- Use a while loop to print the pattern.
- Outer while loop iterate until i>0 is false.
- The first inner while loop prints space if the condition j++<n-i is true.
- The second inner loop prints character if the condition j++<(i*2)-1 is true.
- Display the result.
- Stop
The below example illustrates the implementation of the above algorithm.
//Java Program To Print the Inverted Star Pattern
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int n=sc.nextInt();
int i=n,j;
while(i>0)
{
j=0;
while(j++<n-i)
{
System.out.print(" ");
}
j=0;
while(j++<(i*2)-1)
{
System.out.print("*");
}
System.out.println();
i--;
}
}
}
Enter the number of rows: 7
*************
***********
*********
*******
*****
***
*
Program 3: Print the Inverted Star Pattern
In this program, we will see how to print the inverted star pattern in java using a do-while loop.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare a variable to store the number of rows.
- Ask the user to initialize the variable.
- Use a do-while loop to print the pattern.
- The outer do-while loop iterates until while(āi>0) is false.
- The first inner loop prints space until the condition j++<n-i is false.
- The second inner loop prints the character until the condition j++<i*2-2 is false.
- Display the result.
- Stop
The below example illustrates the implementation of the above algorithm.
//Java Program To Print the Inverted Star Pattern
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create an instance of the Scanner Class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int n=sc.nextInt();
int i=n,j;
do
{
j=0;
do
{
System.out.print(" ");
}while(j++<n-i);
j=0;
do
{
System.out.print("*");
}while(j++<i*2-2);
System.out.println();
}while(--i>0);
}
}
Enter the number of rows: 7
*************
***********
*********
*******
*****
***
*