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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.