Pyramid triangle star pattern using for loop in Java

Pyramid triangle printing in java 


We can display many types of the rectangle, square shapes, many types of tables, pyramid shapes and Floyd’s triangle shapes using java language. 

Here we will learn that “how to display Pyramid triangle”

We will use for loop, while loop in java for this purpose

Program 1
you want to print following pyramid pattern  with a java program. you can use for loop and print();, println(); output method in java
System.out.print();- this method  use to print same line ,
System.out.println();- this method use to print next line ,
  • How to pront this pyramid triangle shape using star in java
                            *
                          ***
                         *****
                       *******
                     *********
                   ***********
                  *************
                 ***************
                *****************
                     

 

class pyramid11{
public static void main(String args[]){
int spacecounter=9,starcounter=1;
for(int row=1; row<=10; row++){         //this is outter for loop
     System.out.println();
      for(int j=1; j<=spacecounter;j++){   // this is a inner for loop
     System.out.print(” “);
                 }
           for(int k=1; k<=starcounter; k++)
        {
       System.out.print(“*”);
          }
       spacecounter–;
       starcounter=starcounter+2;
   }
}
}

When the above code is compiled and executed, it produces the following result:
Example 1
how to print inverted pyramid shape using for loop in scanner class in java?

program 3

import java.util.Scanner;
public class InvertedPyramid
{                                                     // start of class
public static void main (String args[])
{ //start of main method
int rows,i,space,star;                        // variable diclaration
star =0;                                            // variable initiation
Scanner in =new Scanner(System.in);                 // scanner class
System.out.print(“Enter the nummber of rows”);
rows=in.nextInt();                                           // for get input
for(i=rows; i>=1; i–){                                   //outer for loop
                                                             // printing space
for(space=0; space<=rows-i; space++)
{                                                          // inner for loop
System.out.print(” “);
}
                                                              //printing star
  star=0;
  while(star!=(2*i-1))
  {
  System.out.print(“*”);
  star++;                                        //increment statement
  }
  System.out.println();
}
}                                                         //end of main method
}                                                       //end of class
Output
Example 2
we use for loop and while loop for make this output
for and while are keywords in java
Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.