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:
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
we use for loop and while loop for make this output
for and while are keywords in java