Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Java program to create Inverted Pyramid star pattern

Java program to create Inverted Pyramid star pattern

Posted on December 12, 2019December 13, 2019

Table of Contents

  • Java program to create an Inverted Pyramid  star pattern
    • Program to Inverted Pyramid using for loop
    • Program to Inverted Pyramid using while loop
    • Program to Inverted Pyramid using do-while loop

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

Java program to create Inverted Pyramid star pattern
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

Java program to create Inverted Pyramid star pattern
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

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes