Printing stars in java – Java Program to Print Sand Glass Shape Star Pattern

Program to Print Sand Glass Star Pattern

Printing stars in java: In this article we are going to see how to print Sand Glass Shape Star program, Sand Glass Star Pattern In Java, Sandglass Star Program In Java, Sand Glass Pattern In Java, Sandglass Star Pattern, Pattern Printing In Java, Sandglass Star Program In Python, Sand Glass Pattern In Python.

Example-1

When row value=4
 * * * *
  * * *
   * *
    *
    *
   * *
  * * *
 * * * *
Example-2:

When row value=5
* * * * *
 * * * *
  * * *
   * *
    *
    *
   * *
  * * *
 * * * *
* * * * *

Now, let’s see program how to print it.

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first outer 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.
  • Take second outer 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.

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();
    //first outer for loop for printing upper side 
    for ( r= 0; r<= row -1 ; r++)
       {
           //printing spaces
            for (  c=0; c <r; c++)
                  System.out.print(" ");
            // printing stars
            for (  c=r; c<=row -1; c++) 
                 System.out.print("* "); 
            //taking to the new line
            System.out.println();
        } 
        
    // second for loop for printing lowerside 
    for (  r= row -1; r>= 0; r--)
       {
            //printing spaces
            for (  c=0; c< r ;c++)
                System.out.print(" ");
            // printing stars
             for (  c=r; c<=row -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.*;
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 random character
    System.out.print("Enter character : ");
    char sym = s.next().charAt(0);
    //first outer for loop for printing upper side 
    for ( r= 0; r<= row -1 ; r++)
       {
           //printing spaces
            for (  c=0; c <r; c++)
                  System.out.print(" ");
            // printing stars
            for (  c=r; c<=row -1; c++) 
                 System.out.print(sym+" "); 
            //taking to the new line
            System.out.println();
        } 
        
    // second for loop for printing lowerside 
    for (  r= row -1; r>= 0; r--)
       {
            //printing spaces
            for (  c=0; c< r ;c++)
                System.out.print(" ");
            // printing stars
             for (  c=r; c<=row -1; c++)
                System.out.print(sym+" ");
            //taking to the new line
            System.out.println();
        }
    }
}


Output:

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

Explanation :

For First For loop :

Iteration-1

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

First inner for loop will print the space 0  time because loop will iterate up to c<r  time .

Then 2nd inner for loop will print the “* ”   row-1  time , that means 5 time(c=0 to 4) .

So, star will be printed 5 time.

* * * * *

Iteration-2

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<r  time .

Then 2nd inner for loop will print the “* ”   row -1  time , that means 4 time(c=1 to 4)  .

So, star will be printed 4 time.

 * * * *

Iteration-3

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<r  time .

Then 2nd inner for loop will print the “* ”   row -1  time , that means 3 time (c=2 to 4).

So, star will be printed 3 time.

  * * *

Iteration-4

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<r  time .

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

So, star will be printed 2 time.

   * *

Iteration-5

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<r  time .

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

So, star will be printed 1 time.

    *

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

For second outer  for loop

Iteration-1

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

First inner for loop will print the space 4  time because it will iterate upto c<r time .

Then 2nd inner for loop will print the “* ”  c=r to row -1 time , that means 1 time .

Star will be printed 1 time.

        *

Iteration-2

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

First inner for loop will print the space 3  time because it will iterate upto c<r time .

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

Star will be printed 1 time.

      *  *

Iteration-3

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

First inner for loop will print the space 2  time because it will iterate upto c<r time .

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

Star will be printed 3 time.

  *  *  *

Iteration-4

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

First inner for loop will print the space 1  time because it will iterate upto c<r time .

then 2nd inner for loop will print the “* ”  c=r to row -1 time , that means 4  time .

Star will be printed 4 time.

  *  *  * *

Iteration-5

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

First inner for loop will print the space 0 time because it will iterate upto c<r time .

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

Star will be printed 5 time.

*  *  * *  *

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

And the pattern will be like below.

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

C Code:

#include <stdio.h>
int main() {
   int r, row, c ,d;
   printf("Enter rows: ");
   scanf("%d", &row);
  for ( r= 0; r<= row -1 ; r++)
      {
            for (  c=0; c <r; c++)
                  printf(" ");
            for (  c=r; c<=row -1; c++) 
                 printf("* "); 
            printf("\n");
        } 
        // second for loop for printing lowerside 
        for (  r= row -1; r>= 0; r--)
        {
            for (  c=0; c< r ;c++)
                printf(" ");
         
 
    for (  c=r; c<=row -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;
  for ( r= 0; r<= row -1 ; r++)
      {
            for (  c=0; c <r; c++)
                  cout << " " ;
            for (  c=r; c<=row -1; c++) 
                 cout << "* " ;
            cout << "\n" ;
        } 
    // second for loop for printing lowerside 
        for (  r= row -1; r>= 0; r--)
        {
            for (  c=0; c< r ;c++)
                cout << " " ;
             for (  c=r; c<=row -1; c++)
                cout << "* " ;
             cout << "\n" ;

        }
    return 0;
}



Output:

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

Enhance your knowledge:

  1. Print Stars In Java
  2. Star In Java
  3. Printing Stars In Java
  4. Star Program In Java
  5. Star Pattern Program In Java

Related Java Star Pattern Programs: