Print Alphabet Z Number Pattern
In the previous article, we have discussed Java Program to Print Alphabet H Number Pattern
In this article we are going to see how to print alphabet ‘H’ number pattern.
Example-1 When rows value = 5 1 2 3 4 5 4 3 2 1 2 3 4 5
Example-2: When rows value=7 1 2 3 4 5 6 7 6 5 4 3 2 1 2 3 4 5 6 7
Now, let’s see the actual program to print it.
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Approach:
- Enter total number of rows and store it in an integer variable
rows. - Take for loops to iterate the rows and columns.
- After each iteration print a new line.
Java Code to Print Alphabet Z 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;
//THe dimensions for the pattern
int top = 1, bot = 1, dia = rows - 1;
//Prints the top part
//Iterates from 0 to (2 * rows) -1
for (numberOfRows = 0; numberOfRows < rows; numberOfRows++)
System.out.print(top++ + " ");
//Prints a newline
System.out.println();
//Prints the diagonal part
for (numberOfRows = 1; numberOfRows < rows - 1; numberOfRows++)
{
//Inner for loop to print the space
for (numberOfColumns = 0; numberOfColumns < 2 * (rows - numberOfRows - 1); numberOfColumns++)
System.out.print(" ");
System.out.print(dia--);
//Prints a newline
System.out.println();
}
//Prints the bottom part
for (numberOfRows = 0; numberOfRows < rows; numberOfRows++)
System.out.print(bot++ + " ");
}
}
Output Rows : 7 1 2 3 4 5 6 7 6 5 4 3 2 1 2 3 4 5 6 7
C Code to Print Alphabet Z 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;
//THe dimensions for the pattern
int top = 1, bot = 1, dia = rows - 1;
//Prints the top part
//Iterates from 0 to (2 * rows) -1
for (numberOfRows = 0; numberOfRows < rows; numberOfRows++)
printf("%d ", top++);
//Prints a newline
printf("\n");
//Prints the diagonal part
for (numberOfRows = 1; numberOfRows < rows - 1; numberOfRows++)
{
//Inner for loop to print the space
for (numberOfColumns = 0; numberOfColumns < 2 * (rows - numberOfRows - 1); numberOfColumns++)
printf(" ");
printf("%d ", dia--);
printf("\n");
}
//Prints the bottom part
for (numberOfRows = 0; numberOfRows < rows; numberOfRows++)
printf("%d ", bot++);
return 0;
}
Output: Rows : 7 1 2 3 4 5 6 7 6 5 4 3 2 1 2 3 4 5 6 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: