Program to Print Inverted Pascal’s Triangle Star Pattern
Pascal triangle 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 how program printing it.
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Approach:
- Enter total row and store it in an integer variable
row
. - Take first for loop to print all rows.
- Take first inner for loop to print column values i.e., first inner for loop will print all the spaces in the column.
- Take 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 for (r=row;r>=1;r--) { //printing the spaces for(c=row-r;c>=1;c--) System.out.print(" "); //printing the stars for(c=1;c<=r;c++) System.out.print(" * "); // taking to 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 the symbol System.out.print("Enter symbol : "); char symbol = s.next().charAt(0); //counting space for (r=row;r>=1;r--) { //printing the spaces for(c=row-r;c>=1;c--) System.out.print(" "); //printing the stars for(c=1;c<=r;c++) System.out.print(" "+symbol+" "); // taking to new line System.out.println(); } } }
Output: Enter rows : 5 Enter symbol : # # # # # # # # # # # # # # # #
Explanation :
Iteration-1
r=5 (passes the first for loop condition) as it will iterate up to r>=1
times.
First inner for loop will iterate (row-r)
to 1 that means , it will print the space 0 time .
then 2nd inner for loop will iterate up to c<=r
times so, it prints the * 5 time .
So, star will be printed 5 time.
* * * * *
Iteration-2
r=4 (passes the first for loop condition) as it will iterate up to r>=1
times.
First inner for loop will iterate (row-r)
to 1 that means , it will print the space 1 time .
then 2nd inner for loop will iterate up to c<=r
times so, it prints the * 4 time .
So, star will be printed 4 time.
* * * *
Iteration-3
r=3 (passes the first for loop condition) as it will iterate up to r>=1
times.
First inner for loop will iterate (row-r)
to 1 that means , it will print the space 2 time .
then 2nd inner for loop will iterate up to c<=r
times so, it prints the * 3 time .
So, star will be printed 3 time.
* * *
Iteration-4
r=2 (passes the first for loop condition) as it will iterate up to r>=1
times.
First inner for loop will iterate (row-r)
to 1 that means , it will print the space 3 time .
then 2nd inner for loop will iterate up to c<=r
times so, it prints the * 2 time .
So, star will be printed 2 time.
* *
Iteration-5
r=1 (passes the first for loop condition) as it will iterate up to r>=1
times.
First inner for loop will iterate (row-r)
to 1 that means , it will print the space 4 time .
then 2nd inner for loop will iterate up to c<=r
times so, it prints the * 1 time .
So, star will be printed 1 time.
*
Now when r=0, 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 Code:
#include <stdio.h> int main() { int r, row, c ,d; printf("Enter rows: "); scanf("%d", &row); for (r=row;r>=1;r--) { for(c=row-r;c>=1;c--) printf(" "); for(c=1;c<=r;c++) printf(" * "); printf("\n"); } return 0; }
Output: Enter rows : 5 * * * * * * * * * * * * * * *
C++ Code:
#include <iostream> using namespace std; int main() { int row, r , c ,d ; cout << "Enter rows: "; cin >> row; for (r=row;r>=1;r--) { for(c=row-r;c>=1;c--) cout << " "; for(c=1;c<=r;c++) cout << "* "; cout << "\n"; } return 0;
Output: Enter rows : 5 * * * * * * * * * * * * * * *
Related Java Star Pattern Programs: