Java Program to Print Right Pascal’s Triangle Star Pattern

Program to Print Right Pascal’s Triangle Star Pattern

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

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first for loop to print all rows.
  • Take second/inner for loop to print column values.
  • Then go on printing the star symbols according to the iteration.

JAVA Code:

Method-1 : Static Star Character

import java.util.Scanner;
public class Main
{ 
    public static void main(String[] args)
        {
            int row , r, c ;
            // creating Object
            Scanner sc=new Scanner(System.in);
            // entering no. of rows
            System.out.print("Enter Row : ");
            row=sc.nextInt(); 
            for (r= 0; r<= row -1; r++)  
                {  //printing stars
                    for (c=0; c<=r; c++)   
                        System.out.print("* "); 
                    //taking to the new line
                    System.out.println("");   
                }   
 
            for (r=row-1; r>=0; r--)  
                {  
                    // printing stars
                    for(c=0; c <= r-1;c++)  
                        System.out.print("* ");  
                    // taking to the new line 
                    System.out.println("");  
                }  
        }  
}  


Output :

Enter Row : 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

Method-2 : User Input Character

import java.util.Scanner;
public class Main
{ 
    public static void main(String[] args)
        {
            int row , r, c ;
            // creating Object
            Scanner sc=new Scanner(System.in);
            // entering no. of rows
            System.out.print("Enter Row : ");
            row=sc.nextInt(); 
            // entering any character
            System.out.print("Enter Row : ");
            char symbol=sc.next().charAt(0); 
            for (r= 0; r<= row -1; r++)  
                {  //printing stars
                    for (c=0; c<=r; c++)   
                        System.out.print(symbol+" "); 
                    //taking to the new line
                    System.out.println("");   
                }   
 
            for (r=row-1; r>=0; r--)  
                {  
                    // printing stars
                    for(c=0; c <= r-1;c++)  
                        System.out.print(symbol+" ");  
                    // taking to the new line 
                    System.out.println("");  
                }  
        }  
}  


Output:

Enter Row : 5
Enter symbol : !
!
! !
! ! !
! ! ! !
! ! ! ! !
! ! ! !
! ! !
! !
!

Explanation :

Let’s understand the program by going through the detailed explanation.

We have taken row value as 5.

For first Outer loop:

Iteration-1
r=1 (passes the first for loop condition) because it will execute until r<=row
Now r=1 so inner for loop will execute 1 time because it will execute until c<r

Star will be printed one time because inner for loop will be executed only once.

*

Iteration-2
r=2 (passes the first for loop condition) because it will execute until r<=row
Now r=2 so inner for loop will execute 2 times because it will execute until c< r
Star will be printed two times because inner for loop will be executed two times..

* *

Iteration-3
r=3 (passes the first for loop condition) because it will execute until r<=row
Now r=3 so inner for loop will execute 3 times because it will execute until c< r
Star will be printed three times because inner for loop will be executed three times.

* * *

Iteration-4

r=4 (passes the first for loop condition) because it will execute until r<=row
Now r=4 so inner for loop will execute 4 times because it will execute until c< r
Star will be printed three times because inner for loop will be executed four times.

* * * *

Iteration-5

r=5 (passes the first for loop condition) because it will execute until r<=row
Now r=5 so inner for loop will execute 5 times because it will execute until c< r
Star will be printed five times because inner for loop will be executed five times.

* * * * *

For Second Outer loop:

Iteration-1

r=4(passes the first for loop condition) because it will execute until r>0

Now r=4 so inner for loop will execute 4 time because it will execute until c<=r

Star will be printed 5 time because inner for loop will be executed 4 time.

* * * *

Iteration-2

r=3(passes the first for loop condition) because it will execute until r>0

Now r=4 so inner for loop will execute 3 time because it will execute until c<=r

Star will be printed 3 time because inner for loop will be executed only 3 time.

* * *

Iteration-3

r=2(passes the first for loop condition) because it will execute until r>0

Now r=2 so inner for loop will execute 2 time because it will execute until c<=r

Star will be printed 2 time because inner for loop will be executed only twice.

* *

Iteration-4

r=1(passes the first for loop condition) because it will execute until r>0

Now r=1 so inner for loop will execute 1 time because it will execute until c<=r

Star will be printed 1 time because inner for loop will be executed once.

*

Now r =0, so first for loop condition will fail.  So next for loop will not be executed any more.

Now, after end of all iteration we will see the complete pattern is printed on the output screen like this.

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

C Code:

#include <stdio.h>
int main() 
{
   int r, row, c ,d;
   printf("Enter rows: ");
   scanf("%d", &row);
             for (r= 1; r<= row ; r++)  
                {   
                    for (c=0; c<r; c++)   
                        printf("* "); 
                     
                     printf("\n");   
                }   
            for (r=row-1; r>0; r--)  
                {  
                    for(c=1; c <=r;c++)  
                         printf("* ");  
                    
                     printf("\n");  
                }  
                return 0;
        }  

Output : 
Enter Row : 5 
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
*

C++ Code:

#include <iostream>
using namespace std;
int main()
{
   int row, r , c ,d ;
   cout << "Enter  rows: ";
   cin >> row;
   for (r= 1; r<= row ; r++)  
                {   
                    for (c=0; c<r; c++)   
                        cout << "* "; 
                     
                     cout << "\n";   
                }   
            for (r=row-1; r>0; r--)  
                {  
                    for(c=1; c <=r;c++)  
                         cout << "* ";;  
                    
                     cout << "\n";
                }  
                return 0;
    
}
Output : 

Enter Row : 5 
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
*

Related Java Star Pattern Programs: