Java Program to Print Triangle Star Pattern

Program to Print Triangle Star Pattern

In this article we are going to see how to print the triangle  star pattern

Example-1

For rows = 5  
        *
      *  *
    *     *
  *        *
*********
Example-2

For rows = 6  
          *
       *    *
     *        *
   *            *
 *                *
  ***********

Now, let’s see the actual program to print 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 rows.
  • Take one outer for loop to iterate the rows.
  • Take two inner for loops, one to print the space and the other one to print the star.
  • After every iteration print a newline.

JAVA Code:

Method-1 : Static Star Character

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)  
    {  
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Rows : ");
        //Taking input from user
        int r, c, k, rows=scan.nextInt();
        //Outer loop
        for (r=1; r<= rows ; r++)  
        {//Inner loop  
        for (c = r; c < rows ; c++)   
        {  
            System.out.print(" ");  
        }//Inner loop 2     
        for (k = 1; k <= (2*r -1) ;k++)   
        {  
            if(k==1 || r == rows || k==(2*r-1))   
            {  
                System.out.print("*");  
            }  
            else   
            {  
                System.out.print(" ");  
            }  
        }  //To print a newline
            System.out.println("");  
        }  
    }  
}


Enter Rows :  5
      *
   *    *
  *      *
 *        *
*********

Method-2 : User Input Character

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)  
    {  
        int r, c, k;
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Rows : ");
        //Taking input from user
        int rows=scan.nextInt();
        
        System.out.print("Enter any symbol : ");
        //Taking input from user
        char s=scan.next().charAt(0);
        //Outer loop
        for (r=1; r<= rows ; r++)  
        {//Inner loop  
        for (c = r; c < rows ; c++)   
        {  
            System.out.print(" ");  
        }//Inner loop 2     
        for (k = 1; k <= (2*r -1) ;k++)   
        {  
            if(k==1 || r == rows || k==(2*r-1))   
            {  
                System.out.print(s);  
            }  
            else   
            {  
                System.out.print(" ");  
            }  
        }  //To print a newline
            System.out.println("");  
        }  
    }  
}


Output:

Enter Rows : 5
Enter any symbol : @
            @
       @      @
    @            @
 @                  @
@@@@@@@@

Explanation :

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

We have taken rows value as 5.

Iteration-1
r=1, goes into the first inner loop and prints 4 space as c<rows.

It prints one star k==1 and rest space.

    *

Iteration-2
r=2, goes into the first inner loop and prints 3 space as c<rows.

It prints two stars for k==1 and k==(2 * r - 1)) and rest spaces.

   *      *

Iteration-3
r=3, goes into the first inner loop and prints 2 space as c<rows.

It prints two star k==1 and k == (2 * r - 1)) and rest spaces.

  *        *

Iteration-4
r=4, goes into the first inner loop and prints 1 space as c<rows.

It prints two star k==1 and k == (2 * r - 1)) and rest spaces.

 *            *

Iteration-5
r=5, goes out of the first inner loop as c>rows.

It prints 9 stars as r == rows

*********

After this r >rows i.e. 6 so program will come out of the loop.

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 argc, char const *argv[])
{
    printf("Enter rows : ");
    int rows, r, c, k;
    //Taking row as input from user
    scanf("%d", &rows);
    //Outer loop
    for (r = 1; r <= rows; r++)
    { //Inner loop
        for (c = r; c < rows; c++)
        {
            printf(" ");
        } //Inner loop 2
        for (k = 1; k <= (2 * r - 1); k++)
        {
            if (k == 1 || r == rows || k == (2 * r - 1))
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        } //To print a newline
        printf("\n");
    }
    return 0;
}
Output:

Enter rows = 5  
       *
     *   *
   *      *
 *         *
*********

C++ Code:

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    cout << "Enter rows : ";
    int rows, r, c, k;
    //Taking row as input from user
    cin >> rows;
    //Outer loop
    for (r = 1; r <= rows; r++)
    { //Inner loop
        for (c = r; c < rows; c++)
        {
            cout << " ";
        } //Inner loop 2
        for (k = 1; k <= (2 * r - 1); k++)
        {
            if (k == 1 || r == rows || k == (2 * r - 1))
            {
                cout << "*";
            }
            else
            {
                cout << " ";
            }
        } //To print a newline
        cout << endl;
    }
    return 0;
}
Output:

Enter rows = 5  
        *
     *    *
   *        *
 *            *
 *********

Related Java Star Pattern Programs: