Print pattern in java – Java Program to Print Alphabet H Star Pattern

Program to Print Alphabet H Star Pattern

Print pattern in java: In this article we are going to see how to print the alphabet H star pattern. Actually alphabet star pattern in java described very well in addition of this, Print Star Pattern In Java, Star Pattern In Java, Alphabet Pattern Programs In Java, Program To Print Pattern In Java, Java Program To Print Pattern, Pattern Printing In Java, Alphabet Pattern Program In Java, Alphabet Pattern In Java, Print Pattern In Java, Star Pattern Program In Java, Complex Pattern Programs In Java, Star Patterns In Java, Java Pattern Programs, Java Alphabet, Character Pattern Programs In Java, Star Program In Java, Print Alphabet Pattern In Java are discussed in this article and previous one too.

Example-1

When rows : 5

*      *
*      *
******
*      *
*      *
Example-1

When rows : 7

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

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Approach:

  • Enter total row and store it in an integer variable rows.
  • Take one outer for loop to iterate the rows and one inner loop to iterate the columns.
  • Print stars where conditions matches else continue.

JAVA Code:

Method-1: Static Star Character

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

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 size as input from user
    System.out.print("Rows : ");
    int rows = scan.nextInt();
    
    //Taking any character as input from user
    System.out.print("Character : ");
    char h = scan.next().charAt(0);
    
   
    for (int r = 0; r < rows; r++) //Outer Loop
    {
        System.out.print(h);
        for (int c = 0; c < rows; c++) //Inner Loop
        { 
            if (c == rows - 1 || (r == rows / 2))
                System.out.print(h);
            else
                System.out.print(" ");
        }
        System.out.println();
        //Prints a newline
    }
  }
}

Output:

Rows : 5
Character : #

#         #
#         #
######
#         #
#         #

Explanation :

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

We have taken rows value as 5.

Iteration-1
r=0, goes into the inner loop prints two stars, one from the outer loop and other one as  c == rows - 1.

*        *

Iteration-2
r=1, goes goes into the inner loop prints two stars, one from the outer loop and other one as  c == rows - 1.

*        *

Iteration-3
r=2, goes into the inner loop prints five stars, one star from the loop iteration and other four as r == rows / 2.

******

Iteration-4

r=3, goes goes into the inner loop prints two stars, one from the outer loop and other one as  c == rows - 1.

*        *

Iteration-5

r=4, goes into the inner loop prints two stars, one from the outer loop and other one as  c == rows - 1.

*        *

After this r equals rows i.e. 5 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 r, c;
    for (r = 0; r < rows; r++)
    { //Outer Loop
        printf("*");
        for (c = 0; c < rows; c++)
        { //Inner Loop
            if (c == rows - 1 || (r == rows / 2))
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
        //Prints a newline
    }
    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 r, c;
    for (r = 0; r < rows; r++)
    { //Outer Loop
        cout << "*";
        for (c = 0; c < rows; c++)
        { //Inner Loop
            if (c == rows - 1 || (r == rows / 2))
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
        //Prints a newline
    }
    return 0;
}

Output:

Rows : 5

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

Also Refer: Java Program to Print Alphabet L Star Pattern

Try these: 

  • How To Print Pattern In Java
  • How To Print Alphabet Pattern In Java

Related Java Star Pattern Programs: