Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Pyramid triangle star pattern using for loop in Java

Posted on December 7, 2016December 26, 2019

Table of Contents

  • Pyramid triangle printing in java 
  • program 3

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:
Pyramid triangle star pattern using for loop in Java
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
Pyramid triangle star pattern using for loop in Java
Example 2
 
we use for loop and while loop for make this output
for and while are keywords in java
 
Rectangle,Square and Table in Java
Floyd’s Triangle in JaVa
Rectangle, Square in C++
Floyd,s Triangle in C++
Pyramid print in C++
Rectangle, Square in C
Floyd,s Triangle in C
Pyramid print in C
 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes