In this tutorial, we will dicuss pyramid number pattern in Java using for loop
Pyramid Pattern 1
Pyramid pattern 1
Program
import java.util.Scanner;
public class Pyramid_pattern1{
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++){//outer forloop
for (int j=1; j<=(rows-i)*2; j++){
System.out.print(" ");//create initial space for pyramid shape
}
for (int k=i; k>=1; k--){//inner for loops
System.out.print(" "+k);//create left half
}
for (int l=2; l<=i; l++){
System.out.print(" "+l); //create right half
}//end outer for loop
System.out.println();
}
}
}
When the above code is executed, it produces the following results:
import java.util.Scanner;
public class Pyramid_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();//get input from user
System.out.println("");
for(int i=1; i<=rows; i++){
for(int j=0; j=1; l--){
System.out.print(l);//create left half
}
System.out.print("\n");
}
}
}
When the above code is executed, it produces the following results:
import java.util.Scanner;
public class Pyramid_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");
int rows=sc.nextInt();
System.out.println("");
for(int i=1; i<=rows; i++){
for(int j=0; j
When the above code is executed, it produces the following results:
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 produces the following results:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Pyramid Pattern 5
Pyramid number pattern 5
Program
import java.util.Scanner;
public class Pyramid_pattern5{
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(" ");
int i,j,k,l=1;
for(i=1; i<=rows; i++ ){
for(j=1; j<=rows-i; j++){
System.out.print(" ");
}
for(k=1; k<=i; k++,l++){
System.out.print(l+" ");
}
System.out.println(" ");
}
}
}
When the above code is executed, it produces the following results:
import java.util.Scanner;
public class Pyramid_pattern6{
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();//taking rows from user
System.out.println(" ");
System.out.print("Here your pattern\n");
int count=1; //initializing count with 1
int i,j;//variable declaration
for(i=rows; i>=1; i-- ){//outer for loop(parent)
for(j=1; j<=i*2; j++){
System.out.print(" ");//create space from te begining of each row
}//inner for loop 1(child)
for(j=i; j<=rows; j++){//print right part of Piramid
System.out.print(j+" ");
}//inner for loop 2
for(j=rows-1; j>=i; j--){
System.out.print(j+" "); //print left part of Piramid
}//inner for loop 3
System.out.println();
count++;//count increse with 1
}
}
}
When the above code is executed, it produces the following results: