Program to Print Pyramid Star Pattern
How to print pyramid of stars in java: In this article we are going to see how to print the triangle star program.
Example-1 When row value=4 * *** ***** *******
Example-2: When row value=5 * *** ***** ******* *********
Now, let’s see the actual program to print it.
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Approach:
- Enter total row and store it in an integer variable row.
- Take first for loop to print all rows.
- Take second/first inner for loop to print column values i.e., first inner for loop will print all the spaces in the column.
- Take third/second inner for loop to print column values i.e., second inner for loop will print all the stars in the column.
Then go on printing the star symbol according to loop
JAVA Code:
Method-1 : Static Star Character
import java.util.*; public class Main { public static void main(String args[]) { // taking variable for loop iteration and row . int row,r,c,d; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); //counting space d = row - 1; //first ireration for printing uper tringle for (r = 1; r <= row; r++) { //iteration for printing space for (c = 1; c <= d; c++) System.out.print(" "); //decreasing the number of space d--; //printing number of stars for (c = 1; c <= 2 * r - 1; c++) System.out.print("*"); // taking to the new line System.out.println(""); } } }
Output: Enter rows : 5 * *** ***** ******* *********
Method-2 : User Input Character
import java.util.*; public class Main { public static void main(String args[]) { // taking variable for loop iteration and row . int row,r,c,d; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); // entering symbol System.out.print("Enter symbol : "); char symbol=s.next().charAt(0); //counting space d = row - 1; //first ireration for printing uper tringle for (r = 1; r <= row; r++) { //iteration for printing space for (c = 1; c <= d; c++) System.out.print(" "); //decreasing the number of space d--; //printing number of stars for (c = 1; c <= 2 * r - 1; c++) System.out.print(symbol); // taking to the new line System.out.println(""); } } }
Output: Enter rows : 5 Enter symbol : % % %%% %%%%% %%%%%%% %%%%%%%%%
Explanation :
Take a variable to counting space i.e., d
.
Assign d= row-1
.
Iteration-1
r=1 (passes the first for loop condition) as it will iterate up to r<=row
times.
First inner for loop will print the space d
time that means 4 times.
Then space will decrease 1 time (d=3)
then 2nd inner for loop will print the “* ” 2* r -1
time , that means 1 time .
So, star will be printed one time.
*
Iteration-2
r=2(passes the first for loop condition) as it will iterate up to r<=ro
w times.
First inner for loop will print the space d
time that means 3 times.
Then space will decrease 1 time .(d=2)
then 2nd inner for loop will print the “* ” 2* r -1
time , that means 3 time .
Star will be printed 3 times.
* * *
Iteration-3
r=3 (passes the first for loop condition) as it will iterate up to r<=row
times.
First inner for loop will print the space d
time that means 2 times.
Then space will decrease 1 time .(d=1)
then 2nd inner for loop will print the “* ” 2* r -1
time , that means 5 time .
Star will be printed 5 times.
* * * * *
Iteration-4
r=4 (passes the first for loop condition) as it will iterate up to r<=row
times.
First inner for loop will print the space d
time that means 1 time.
Then space will decrease 1 time .(d=0)
then 2nd inner for loop will print the “* ” 2* r -1
time , that means 7 time .
Star will be printed 7 times.
* * * * * * *
Iteration-5
r=5(passes the first for loop condition) as it will iterate up to r<=row
times.
First inner for loop will print the space d
time that means 0 time because it fails the condition c<=d
.
Then space will decrease 1 time .(d=-1)
then 2nd inner for loop will print the “* ” 2* r -1
time , that means 9 time .
Star will be printed 7 times
* * * * * * * * *
Now when r=6, first for loop condition will fail so no other loops will be executed.
At last, we will see a pattern like this as output.
* *** ***** ******* *********
C Program:
#include <stdio.h> int main() { int r, row, c ,d; printf("Enter rows: "); scanf("%d", &row); d = row - 1; for (r = 1; r <= row; r++) { for (c = 1; c <= d; c++) printf(" "); d--; for (c = 1; c <= 2 * r - 1; c++) printf("*"); printf("\n"); } return 0; }
Output: Enter rows : 5 * *** ***** ******* *********
C++ Program:
#include <iostream> using namespace std; int main() { int row, r , c ,d ; cout << "Enter rows: "; cin >> row; d=row-1; for (r = 1; r <= row; r++) { for (c = 1; c <= d; c++) cout << " "; d--; for (c = 1; c <= 2 * r - 1; c++) cout << "*"; cout << "\n"; } return 0; }
Output: Enter rows: 5 * *** ***** ******* *********
Related Java Star Pattern Programs: