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
  • Related

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

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes