Java Program to Print Alphabet H Number Pattern

Printing Alphabet H Number Pattern

In the previous article, we have discussed Java Program to Print Double Headed Arrow Number Pattern

In this article we are going to see how to print alphabet ‘H’  number pattern.

Example-1

When rows value = 5

1          5
2          4
3          3
4          2
5 4 3 2 1
4          2
3          3
2          4
1          5
Example-2:

When rows value=7

1                7
2                6
3                5
4                4
5                3
6                2
7 6 5 4 3 2 1
6                2
5                3
4                4
3                5
2                6
1                7

Now, let’s see the actual program 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 number of rows and store it in an integer variable rows.
  • Take one outer for loop to iterate the rows.
  • Take one inner for loop to print both space and number.
  • After each iteration print a new line.

Java Code to Print Alphabet H Number Pattern

import java.util.Scanner;
class Main
{

public static void main(String[] args)
{
    //Create a new Scanner object
    Scanner scan = new Scanner(System.in);

    //Taking total number of rows as input from user
    System.out.print("Rows : ");
    int rows= scan.nextInt();

    //Row and column are the iterators and counter to print
   int numberOfRows, numberOfColumns;

   int left = 0, mid = rows - 1, right = rows + 1;

   //Outer for loop iterates rows
   //Iterates from 0 to (2 * rows) -1
   for (numberOfRows = 0; numberOfRows < 2 * rows - 1; numberOfRows++)
   {
      //Prints the left side
      if (numberOfRows < rows)
         System.out.print(++left);
      else
         System.out.print(--left);
      //Inner for looop to print the mid
      //Iterates from 1 to rows-1
      for (numberOfColumns = 1; numberOfColumns < rows - 1; numberOfColumns++)
      {
         if (numberOfRows != rows - 1)
            System.out.print("  ");
         else
            System.out.print(" "+mid--);
      }
      //Prints the right side
      if (numberOfRows < rows)
         System.out.print(" "+ --right);
      else
         System.out.print(" "+ ++right);
      //Prints a newline
      System.out.println();
   }
}
}

Output

Rows : 7

1                7
2                6
3                5
4                4
5                3
6                2
7 6 5 4 3 2 1
6                2
5                3
4                4
3                5
2                6
1                7

C Code to Print Alphabet H Number Pattern

#include <stdio.h>

int main()
{
   //Taking total number of rows as input from user
   printf("Rows : ");
   int rows;
   scanf("%d", &rows);

   //Row and column are the iterators and counter to print
   int numberOfRows, numberOfColumns;

   int left = 0, mid = rows - 1, right = rows + 1;

   //Outer for loop iterates rows
   //Iterates from 0 to (2 * rows) -1
   for (numberOfRows = 0; numberOfRows < 2 * rows - 1; numberOfRows++)
   {
      //Prints the left side
      if (numberOfRows < rows)
         printf("%d", ++left);
      else
         printf("%d", --left);
      //Inner for looop to print the mid
      //Iterates from 1 to rows-1
      for (numberOfColumns = 1; numberOfColumns < rows - 1; numberOfColumns++)
      {
         if (numberOfRows != rows - 1)
            printf("  ");
         else
            printf(" %d", mid--);
      }
      //Prints the right side
      if (numberOfRows < rows)
         printf(" %d", --right);
      else
         printf(" %d", ++right);
      //Prints a newline
      printf("\n");
   }
   return 0;
}
Output:

Rows : 7

1                7
2                6
3                5
4                4
5                3
6                2
7 6 5 4 3 2 1
6                2
5                3
4                4
3                5
2                6
1                7

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Number Pattern Programs: