Java Program to Print Digit 0 Star Pattern

Program to Print Digit 0 Star Pattern

In this article we are going to see how to print the number ‘0’ star pattern.

Example-1

When row value=8

********
*         *
*         *
*         *
*         *
*         *
*         *
********
Example-2:

When row value=5

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

Now, let’s see the actual program to print it.

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Approach:

  • Enter total row and store it in an integer variable rows.
  • Take columns = rows.
  • Take outer for loop to iterate the rows.
  • Take inner for loop to print space and star.
  • After each iteration print a newline.

JAVA Code:

Method-1 : Static Star Character

import java.util.Scanner;
class pattern
{
    public static void main(String[] args) 
    {
        System.out.print("Rows : ");
        Scanner scan = new Scanner(System.in);
        int rows = scan.nextInt();
        //Taking rows as input from user
        int columns = rows;
        //Taking columns from rows
        int r,c;
        //Iterators
        for (r = 1; r <= rows; r++) 
        {//Outer loop to iterate rows
        for (c = 1; c <= columns; c++) 
        {//Inner loop to iterate columns
            if(r == 1 || r == rows ) {
                System.out.print("*");  
            } else if(c == 1 || c == columns ) {
                System.out.print("*");  
            } else {
                System.out.print(" ");  
            }   
        }
        System.out.println();
    }
    }
}

Output

Rows : 5

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

Method-2 : User Input Character

import java.util.Scanner;
class Main
{
    public static void main(String[] args) 
    {
        System.out.print("Rows : ");
        Scanner scan = new Scanner(System.in);
        int rows = scan.nextInt();
        //Taking rows as input from user
        int columns = rows;
        //Taking columns from rows
        
        System.out.print("character : ");
        char zero=scan.next().charAt(0);
        //Taking any random character input
        
        int r,c;
        //Iterators
        for (r = 1; r <= rows; r++) 
        {//Outer loop to iterate rows
        for (c = 1; c <= columns; c++) 
        {//Inner loop to iterate columns
            if(r == 1 || r == rows ) {
                System.out.print(zero);  
            } else if(c == 1 || c == columns ) {
                System.out.print(zero);  
            } else {
                System.out.print(" ");  
            }   
        }
        System.out.println();
    }
    }
}

Output

Rows : 5
Character : 0

00000
0      0
0      0
0      0
00000

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 inner loop prints five stars as r == 1 .

*****

Iteration-2
r=2, goes into the inner loop prints one star for c == 1, then prints three spaces as conditions do not match. Then prints another star as c == columns.

*       *

Iteration-3
r=3, goes into the inner loop prints one star for c == 1, then prints three spaces as conditions do not match. Then prints another star as c == columns.

*       *

Iteration-4

r=4, goes into the inner loop prints one star for c == 1, then prints three spaces as conditions do not match. Then prints another star as c == columns.

*       *

Iteration-5

r=5, goes into the first inner loop prints five stars for c == 1 || c == columns.

*****

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[])
{
    int rows;
    printf("Rows : ");
    scanf("%d", &rows);
    //Taking rows as input from user
    int columns = rows;
    //Taking columns from rows
    int r, c;
    //Iterators
    for (r = 1; r <= rows; r++)
    { //Outer loop to iterate rows
        for (c = 1; c <= columns; c++)
        { //Inner loop to iterate columns
            if (r == 1 || r == rows)
            {
                printf("*");
            }
            else if (c == 1 || c == columns)
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
}
Output

Rows : 5

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

C++ Code:

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    int rows;
    cout << "Rows : ";
    cin >> rows;
    //Taking rows as input from user
    int columns = rows;
    //Taking columns from rows
    int r, c;
    //Iterators
    for (r = 1; r <= rows; r++)
    { //Outer loop to iterate rows
        for (c = 1; c <= columns; c++)
        { //Inner loop to iterate columns
            if (r == 1 || r == rows)
            {
                printf("*");
            }
            else if (c == 1 || c == columns)
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }
        cout << endl;
    }
    return 0;
}
Output

Rows : 5

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

Related Java Star Pattern Programs: