Java

Triangle Pyramid program in Java using NetBeans

Triangle Pyramid Program in java

Triangle Pyramid program in Java using NetBeans- in this tutorial I will provide a solution on how to make a Triangle Pyramid pattern in java with the (*) symbol using NetBeans.


Related Articles:

How to write a Triangle Pyramid program in Java using NetBeans

To write a Triangle Pyramid pattern program, we will use a looping function using for loops, and for input using Scanner, so the user is required to input the value used as the size or height of the pyramid.



In the program, there are 3 for loops, and each for loop has a different task, namely to count the number of lines, the number of spaces, and the number of symbols (*), for that, try to pay attention to the following source code:

package com.mycompany.javabasics;
import java.util.Scanner;

/**
 *
 * @author Fawadkhan
 */
public class PyramidExample {
    public static void main(String[] args){
        //Instance/Input Scanner Object
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the Height: ");
        int Triangle = input.nextInt(); //Get input from user
        
        for(int h=1; h<=Triangle; h++){
            //Calculating the Total Height of the Pyramid
            
            for(int s=h; s<=Triangle; s++){
               //Calculating Number of Spaces per Line
                System.out.print(" ");
            }
            
            for(int star=0; star<=(h*2)-2; star++){
                //Count the Number of Stars per Row
                System.out.print("*");
            }
            
            System.out.println(); //Create a New Line
        }
    }

    
}

output:

Pyramid program in Java


Triangle Pyramid program Explanation:

If you are still confused about the program, I will explain the details of the algorithm and its logic so that you can understand it.

There we use input from the user to determine the Height value of the pyramid, to make a triangle shape, we use 3 for loops, in the for loops there are 3 variables, namely h (height), s (space), and star.

looping starts from high, then inside it nests 2 for loops, namely to count the spaces and the number of stars, on the first line, the program will calculate the number of spaces first, the value in the variable s follows the value of the variable h, so that the star arrangement looks neat and is located in the middle.


After finishing counting the number of spaces in the first line, then the program will count the number of stars in the second for loops, in the for loops the most important thing is the formula star<=(h*2)-2, to adjust the number of stars per line, in the for loops will score 1 star.

Finally, after that the program will run the System.out.println() function to create a new line and the first loop is complete, then the second loop, and so on until the number of inputs (height) has been determined.

That’s a brief explanation of the Triangle Pyramid program that I made, I hope it can be useful for you.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button