Java Program to Print Pascal’s Triangle Star Pattern

Program Pascal’s Triangle Star Pattern

In this article we are going to see how to print the Pascal’s 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.

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

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

Java Program to Print Pyramid Star Pattern

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   // taking variable for loop iteration and row .
        int row,r,c;
        //creating object 
        Scanner s = new Scanner(System.in);
        // entering the number of row
        System.out.print("Enter rows : ");
        row = s.nextInt();
         //iteration for no. of row 
       for (r=0; r<row; r++) 
        {
            //iteration for printing space
            for (c=row-r; c>1; c--)
                System.out.print(" ");
            // iteration for printing stars
            for (c=0; 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;
        //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);
         //iteration for no. of row 
       for (r=0; r<row; r++) 
        {
            //iteration for printing space
            for (c=row-r; c>1; c--)
                System.out.print(" ");
            // iteration for printing stars
            for (c=0; c<=r; c++ )
                System.out.print(symbol+" ");
            //taking to new line
            System.out.println();
        }
    } 
}


Output:

Enter rows : 5
Enter symbol : @
        @
      @ @
    @ @ @
  @ @ @ @
@ @ @ @ @

Explanation :

Iteration-1

r=0 (passes the first for loop condition) as it will iterate up to r<row times.

First inner for loop will print the space (row-r) time that means 5 times.

then 2nd inner for loop will print the * one time because it will iterate up to c<=r times.

So, Star will be printed one time.

     *

Iteration-2

r=1

first inner for loop will print the space (row-r) time that means 4 times.

then 2nd inner for loop will print the * two times because it will iterate up to c<=r times.

Star will be printed 2 times.

   * *

Iteration-3

r=2 (passes the first for loop condition) as it will iterate up to r<row times.

First inner for loop will print the space  (row-r) time that means 3 times.

Then 2nd inner for loop will print the * three times because it will iterate up to c<=r times.

Star will be printed 3 times.

  * * *

Iteration-4

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  (row-r) time that means 2 times.

Then 2nd inner for loop will print the * four times because it will iterate up to c<=r times.

Star will be printed 4 times.

  * * * *

Iteration-5

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  (row-r) time that means 1 times.

Then 2nd inner for loop will print the * five times because it will iterate up to c<=r times.

Star will be printed 5 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 Code:

#include <stdio.h>
int main() {
   int r, row, c = 0;
   printf("Enter rows: ");
   scanf("%d", &row);
   for (r=0; r<row; r++) 
        {
            for (c=row-r; c>1; c--)
                printf(" ");
            for (c=0; 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 ;

   cout << "Enter  rows: ";
   cin >> row;
      for (r=0; r<row; r++) 
        {
            for (c=row-r; c>1; c--)
                cout << " ";
            for (c=0; c<=r; c++ )
                cout << "* ";
            cout << "\n";
        }
    return 0;
    
}
Output:

Enter rows : 5
    * 
   * * 
  * * * 
 * * * * 
* * * * *

Related Java Star Pattern Programs: