Java Program to Print Digit 1 Star Pattern

Program to Print Digit 1 Star Pattern

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

Example-1

When row value=8

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

When row value=5

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

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

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Approach:

  • Enter total row and store it in an integer variable rows.
  • Take columns = rows +1.
  • 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 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+1, midColumn = (rows/2+1);
        //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 && c < midColumn ) {
                System.out.print("*");  
            } else if(c == midColumn ) {
                System.out.print("*");  
            } else if(r == rows ) {
                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) 
    {
        
         Scanner scan = new Scanner(System.in);
         
        //Taking rows as input from user
        System.out.print("Rows : ");
        int rows = scan.nextInt();
        
        //Taking any character as input from user
        System.out.print("Character : ");
        char one = scan.next().charAt(0);
       
        int columns = rows+1, midColumn = (rows/2+1);
        //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 && c < midColumn ) {
                System.out.print(one);  
            } else if(c == midColumn ) {
                System.out.print(one);  
            } else if(r == rows ) {
                System.out.print(one);  
            } else {
                System.out.print(" ");  
            }   
        }
        System.out.println();
    }
    }
}
Output

Rows : 5
Character : 1

111 
    1
    1
    1
111111

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 two stars as r == 1 && c < midColumn and one star for c == midColumn. Rest space is printed for remaining iterations.

***

Iteration-2
r=2, goes into the inner loop prints two empty spaces(‘ ‘)  as it does not meet any the conditions for first two iterations. Then prints a star for c == midColumn. Rest space is printed for remaining iterations.

    *

Iteration-3
r=3, goes into the inner loop prints two empty spaces(‘ ‘)  as it does not meet any the conditions for first two iterations. Then prints a star for c == midColumn. Rest space is printed for remaining iterations.

    *

Iteration-4

r=4, goes into the inner loop prints two empty spaces(‘ ‘)  as it does not meet any the conditions for first two iterations. Then prints a star for c == midColumn. Rest space is printed for remaining iterations.

    *

Iteration-5

r=5, goes into the first inner loop prints six stars for 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[])
{
    int rows;
    printf("Rows : ");
    scanf("%d", &rows);
    //Taking rows as input from user
    int columns = rows + 1,
        midColumn = (rows / 2 + 1);
    //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 && c < midColumn)
            {
                printf("*");
            }
            else if (c == midColumn)
            {
                printf("*");
            }
            else if (r == rows)
            {
                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 + 1,
        midColumn = (rows / 2 + 1);
    //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 && c < midColumn)
            {
                cout << "*";
            }
            else if (c == midColumn)
            {
                cout << "*";
            }
            else if (r == rows)
            {
                cout << "*";
            }
            else
            {
                cout << " ";
            }
        }
        cout << endl;
    }
    return 0;
}

Output

Rows : 5

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

Related Java Star Pattern Programs: