pyramid triangle

Print Double pyramid star pattern in Java

Print Double pyramid star pattern in Java

In this tutorial, we will discuss  the concept of print Double pyramid star pattern in Java

In this post, we will learn how to create double (integrated) pyramid star pattern in Java language.

We can use for loop, while loop or do while loop to display different integrated star patterns.

 

Here, we will use for loop to print different integrated pyramid star patterns in this programs

Program 1

Integrated pyramid star pattern 1

import java.util.Scanner;
class DoublePyramid1{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the number of rows\n");
int rows=scan.nextInt();//reads rows from user
for(int i=1; i<=rows; i++){    //print upper part
    for(int j=i; j<=rows; j++){
      System.out.print("*"+" ");//print star with space
}
System.out.println();
}

for(int i=rows-1; i>=1; i--){  //print lower part
    for(int j=i; j<=rows; j++){
      System.out.print("*"+" ");//print star with space
}
System.out.println();
        }
    }
}

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

Enter the number of rows
6
* * * * * *
* * * * *
* * * *
* * * 
* * 
*
* * 
* * * 
* * * *
* * * * *
* * * * * *

 

Program 2

Integrated pyramid star pattern 2

import java.util.Scanner;
class DiamondNumber4{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the number of rows: ");
int rows=scan.nextInt();//reads rows from user

for(int i=1; i<=rows; i++){    //print upper part
    for(int j=1; j<i; j++){
      System.out.print(" ");//print space
}
for(int j=i; j<=rows; j++){
      System.out.print("*");//print star
}
System.out.println();
}

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

for(int j=i; j<=rows; j++){  //print lower part
      System.out.print("*");//print star
}
System.out.println();
        }
    }
}

 

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

Enter the number ofrows: 6
******
  *****
    ****
      ***
        **
          *
        **
      ***
    ****
  *****
******

 

Program 3

Integrated pyramid star pattern 3

import java.util.Scanner;
class DoublePyramid2{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the number of rows: ");
int rows=scan.nextInt();//reads rows from user
for(int i=1; i<=rows; i++){    //print upper part
    for(int j=1; j<=i; j++){
      System.out.print(" ");
}
for(int k=i; k<=rows; k++){
      System.out.print("*"+" ");//print star with space
}
System.out.println();
}

for(int i=rows-1; i>=1; i--){  //print lower part
    for(int j=1; j<=i; j++){
      System.out.print(" ");
}
for(int k=i; k<=rows; k++){
      System.out.print("*"+" ");//print star with space
}
System.out.println();
        }
    }
}

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

Enter the number of rows: 6
*   *   *   *   *   *
  *   *   *   *   *
    *   *   *   *
      *   *   *
        *   *
          *
        *   *
      *   *   *
    *   *   *   *
  *   *   *   *   *
*   *   *   *   *   *

 

Program 4

Integrated pyramid star pattern 4

import java.util.Scanner;
class DoublePyramid4{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the number of rows: ");
int rows=scan.nextInt();//reads rows from user
for(int i=1; i<=rows; i++){    //print upper part
    for(int j=1; j<=rows-i; j++){
      System.out.print(" ");
}
for(int k=1; k<=i; k++){
      System.out.print("*"+" ");//print star with space
}
System.out.println();
}

for(int i=1; i<=rows-1; i++){  //print lower part
    for(int j=1; j<=i; j++){
      System.out.print(" ");
}
for(int k=1; k<=rows-i; k++){
      System.out.print("*"+" ");//print star with space
}
System.out.println();
        }
    }
}

 

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

Enter the number of rows: 6
         *
       *  *
     *  *  *
   *  *  *  *
  *  *  *  * *
*  *  *  *  * *
  *  *  *  *  *
    *  *  *  *
      *  *  * 
        *  *
          *

 

Similar post

 Intergrated pyramid star pattern in C

Intergrated pyramid star pattern in C++

 

Suggested for you

Operator in Java language

Data type and variable in Java language

For loop in Java language

Nested for loop in Java language

Class and main method in Java language

 

 

 

 

program to Double pyramid star pattern in C++
C code to print Double pyramid star pattern
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago