In this Java tutorial we are going to solve the Reverse Pyramid triangle pattern using the concept of nested for loop.
Java pattern's questions are often asked in interview to check the coding skills,logic and looping concept.
To print the following pattern we will be required 3 for loop's,first one to print the spaces,second and third to print the star's.
So with the combination of this nested loop we can acheive Reverse or Inverted Pyramid pattern.
class Inverted_Pyramid { public static void main(String[] args) { for(int i=1;i<=8;i++){ for (int j=1;j<i;j++){ //for loop to print space's System.out.print(" "); } for(int k=8;k>=i;k--){ //for loop to print star's System.out.print("*"); } for(int l=7;l>=i;l--){ //for loop to print star's System.out.print("*"); } //after successful iteration the cursor will point at next line. System.out.println(); } } }
OUTPUT :
***************
*************
***********
*********
*******
*****
***
*
Hope you understood the concept and code clearly,if not do comment below .
Submitted by Sohail Chand Kalyani (sohailkalyani)
Download packets of source code on Coders Packet
Comments