Java Program to Print Diamond Shape Star Pattern

Program to Print Diamond Shape Star Pattern

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 program to  how print it.

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first outer 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/2nd inner for loop to print column values i.e., second inner for loop will print all the stars in the column.
  • Take second outer 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/2nd inner for loop to print column values i.e., second inner for loop will print all the stars in the column.

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("");
        }
        //declaring space as one .
        d = 1;
        //2nd ireration for printing lower inverted tringle 
        for (r = 1; r <= row - 1; r++) 
        {
            //iteration for printing space 
            for (c = 1; c <= d; c++) 
                System.out.print(" ");
            //increasing the number of space
            d++;
            //printing number of stars
            for (c = 1; c <= 2 * (row - 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();
    // enter any character
    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("");
        }
        //declaring space as one .
        d = 1;
        //2nd ireration for printing lower inverted tringle 
        for (r = 1; r <= row - 1; r++) 
        {
            //iteration for printing space 
            for (c = 1; c <= d; c++) 
                System.out.print(" ");
            //increasing the number of space
            d++;
            //printing number of stars
            for (c = 1; c <= 2 * (row - 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<=row 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 9 times.

* * * * * * * * *

Now when r=6, first for loop condition will fail so no other loops will be executed.

For second outer for loop .

Assign d= 1 .

Iteration-1

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

First inner for loop will print the space 1  time because loop will iterate up to c<=d time .

Then space will increase 1 time .(d=2)

then 2nd inner for loop will print the “* ”  2* (row -r)-1 time , that means 7 time .

So, star will be printed 7 time.

 * * * * * * *

Iteration-2

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

First inner for loop will print the space 2  time because loop will iterate up to c<=d time .

Then space will increase 1 time .(d=3)

then 2nd inner for loop will print the “* ”  2* (row -r)-1 time , that means 5 time .

So, star will be printed 5 time.

  * * * * *

Iteration-3

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

First inner for loop will print the space 3  time because loop will iterate up to c<=d time .

Then space will increase 1 time .(d=4)

then 2nd inner for loop will print the “* ”  2* (row -r)-1 time , that means 3 time .

So, star will be printed 3 time.

* * *

Iteration-4

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

First inner for loop will print the space 4  time because loop will iterate up to c<=d time .

Then space will increase 1 time .(d=5)

then 2nd inner for loop will print the “* ”  2* (row -r)-1 time , that means 1 time .

So, star will be printed 1 time.

    *

Now when r=5 , second outer 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);
   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");
        }
        d = 1;
        for (r = 1; r <= row - 1; r++) 
        {
             for (c = 1; c <= d; c++) 
                printf(" ");
            d++;
            for (c = 1; c <= 2 * (row - r) - 1; 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;
  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";
        }
        d = 1;
 
        for (r = 1; r <= row - 1; r++) 
        {
             for (c = 1; c <= d; c++) 
                cout << " ";
            d++;
            for (c = 1; c <= 2 * (row - r) - 1; c++) 
               cout << "*";
            cout << "\n";
        }
    return 0;
    
}


Output:

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

Related Java Star Pattern Programs: