Codeforcoding

Inverted Pyramid number pattern in Java

Inverted Pyramid number pattern in Java

In tis tutorial, we will discuss about Inverted Pyramid number pattern in Java

Here we are going to learn about how to create  Pyramid pattern in Java

Code to inverted Pyramid  pattern 1

Inverted number Pyramid 1

Program 1

import java.util.Scanner; 
public class Pyramid_pattern4{
public static void main(String args[]){
Scanner sc=new Scanner(System.in); //Scanner class in java
System.out.print("Enter the rows you want");
int rows=sc.nextInt();
System.out.println("");

for (int i=1; i<=rows; i++){
  for (int j=1; j<=rows; j++){
  System.out.print(" ");
  }
 rows--;
for (int k=1; k<=i; k++){
  System.out.print(i+" ");           
}					                           
System.out.println();
}
                                                   
}


}

When the above code is executed, it produced following result

1 2 3 4 5 6 7 8 9
   1 2 3 4 5 6 7 
      1 2 3 4 5 
         1 2 3
            1

 

 

Code to inverted Pyramid  pattern 2

Inverted Pyramid 2

Program 2

import java.util.Scanner; 
public class Invert_pattern2{
public static void main(String args[]){
Scanner sc=new Scanner(System.in); //Scanner class in java
System.out.print("Enter the rows you want");
int rows=sc.nextInt();
System.out.println("");

for (int i=rows; i>=1; i--){
  for (int j=0; j<=rows-i; j++){
  System.out.print(" ");
  }//print space
 int count=0;
while(count != (2*i-1)){
  System.out.print(i+"");  
count++;  
}					                           
System.out.println();
}
                                                   
}
}

When the above code is executed, it produced following result

99999999999999999
  888888888888888
    7777777777777
      66666666666
        555555555
          4444444
            33333
              222
                1

 

Code to inverted Pyramid  pattern 3

Inverted Pyramid number pattern in Java
Inverted Pyramid pattern 3

Program 3

import java.util.Scanner; 
public class Invert_pattern3{
public static void main(String args[]){
Scanner sc=new Scanner(System.in); //Scanner class in java
System.out.print("Enter the rows you want: ");
//Taking input(rows value) from user
int rows=sc.nextInt();
System.out.println("");
int count=rows;//initializing count with rows
for (int i=0; i<=rows; i++){
  for (int j=1; j<=i*2; j++){
  System.out.print(" ");
  }//print space at the bigining of each row
 
for(int j=1; j<=count; j++) {
System.out.print(j+" ");//print left part of pyramid
}
for(int j=count-1; j>=1; j--) {
System.out.print(j+" "); //print right part of pyramid
}
  System.out.println();   
count--;  
          }
      }
}

When the above code is executed, it produced following result

1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
   1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
      1 2 3 4 5 6 7 6 5 4 3 2 1
         1 2 3 4 5 6 5 4 3 2 1
            1 2 3 4 5 4 3 2 1
               1 2 3 4 3 2 1
                  1 2 3 2 1
                     1 2 1
                        1

 

Code to inverted Pyramid  pattern 4

Inverted Pyramid pattern – 4

Program 4

import java.util.Scanner;
public class Reverse_Pyramid{
public static void main(String args[]){
int rows;
Scanner s=new Scanner(System.in);
System.out.println("Enter number of rows");
rows=s.nextInt();
for(int i=1; i<=rows; i++){//do for rows in pyramid
   for(int j=1; j<=i; j++){
     System.out.print(" ");//print space

}
for(int k=i; k<=rows; k++){
     System.out.print(k+" ");

}
System.out.println();
}

}

}

When the above code is executed, it produced following result

1 2 3 4 5 6 7 8
 2 3 4 5 6 7 8
  3 4 5 6 7 8
   4 5 6 7 8
    5 6 7 8
     6 7 8
      7 8
       8

 

Similar post

Floyd’s triangle number pattern using for loop in C

Floyd’s triangle pattern using nested for loop in Java

Floyd’s triangle pattern using nested while loop in Java

Hollow pyramid triangle pattern in C++ language

Pyramid star pattern in Cpp

Pyramid star pattern in C

Pyramid star pattern in Java 

Rhombus pattern in Java using for loop

Rhombus pattern in C using while loop

Rhombus pattern in C++ using do-while loop

 

Suggested for you

for loop in Java

while loop in Java

for loop in C language

while loop in C language

while loop in cpp language

For loop in Cpp language

Nested for loop in C++ language

Nested for loop in Java language

Nested for  loop in C language

Nested for loop in Python language

 

 

 

Floyd's triangle number pattern Using nested while loop in Java
CPP program to print Floyd's triangle number patterns
Exit mobile version