java

Java program to create Inverted Pyramid star pattern

Java program to create an Inverted Pyramid  star pattern

In this article, we will discuss the Java program to create an Inverted Pyramid  star pattern – using loops

In this post, we will learn how to create an Inverted Pyramid star pattern using for, while and do-wile loop in Java language

Program to Inverted Pyramid using for loop

Program 1

This program allows the user to enter the number of rows and the symbol then the program displays a full inverted pyramid star pattern with the given symbol using for loop in java language

 

import java.util.*;
class ReverseStarPyramid{
public static void main(String args[]){
int i,j,rows;//declare integer variable
char ch;//declare character variable
Scanner s=new Scanner(System.in);//create scanner object
System.out.println("Enter value for row: ");
rows=s.nextInt();//read number input from user
System.out.println("Enter symbol as you wish: ");
ch=s.next().charAt(0);//read number input from user
for(i=rows; i>0; i--){//outer for loop 
  for(j=0; j<rows-i; j++){//inner for loop 
  System.out.print(" ");//print star
  
}
for(j=0; j<(i*2)-1; j++){//inner for loop 2
  System.out.print(ch);//print star
  
}
System.out.println(" ");//move to next line
  
}
}
}

When the above code is executed, it produces the following result

Inverted Pyramid

Program to Inverted Pyramid using while loop

Program 2

This program allows the user to enter the number of rows and the symbol then the program displays a full inverted pyramid star pattern with the given symbol using while loop in java language

import java.util.*;
class ReverseStarPyramid1{
public static void main(String args[]){
int i,j,rows;//declare integer variable
char ch;//declare character variable
Scanner s=new Scanner(System.in);
//create scanner object for input
System.out.println("Enter value for row: ");
rows=s.nextInt();//Read input  for rows from the user
System.out.println("Enter symbol as you wish: ");
ch=s.next().charAt(0);Read input  for symbol from the user
i=rows;
while( i>0){
  j=0; 
  while(j<rows-i){
  System.out.print(" ");
   j++;
}
j=0; 
while(j<(i*2)-1){
  System.out.print(ch);
  j++;
}
System.out.println(" ");
  i--;
}
}
}

When the above code is executed, it produces the following result

Inverted Pyramid

Program to Inverted Pyramid using do-while loop

Program 3

This program allows the user to enter the number of rows and the symbol then the program displays a full inverted pyramid star pattern with the given symbol using the do-while loop in java language

import java.util.*;
class ReverseStarPyramid2{
public static void main(String args[]){
int i,j,rows;//declare integer variable
char ch;//declare character variable
Scanner s=new Scanner(System.in);
//create scanner object for input
System.out.println("Enter value for row: ");
rows=s.nextInt();//Read input from user as number
System.out.println("Enter symbol as you wish: ");
ch=s.next().charAt(0);Read input  for symbol from the user
i=rows;
do{//outer do while loop for rows
  j=0; 
  do{//inner do while loop to space
  System.out.print(" ");//print space
   j++;
}while(j<=rows-i);
j=0; 
do{
  System.out.print(ch);//print star
  j++;
}while(j<(i*2)-1);
System.out.println(" ");
  i--;
}while( i>0);
}
}

When the above code is executed, it produces the following result

Suggested post you

For loop in Java

While loop in Java

Do-while loop in Java

Nested for loop in Java

Nested while loop in Java

 

Similar post

C pyramid star pattern program – using loops

C++ pyramid star pattern program – using loops

Java pyramid star pattern program – using loops

C program to create an Inverted Pyramid star pattern

C++ program to create an Inverted Pyramid star pattern

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

5 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

5 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

6 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

6 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

6 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

6 months ago

This website uses cookies.