Printing Alphabet N Number Pattern
In the previous article, we have discussed Java Program to Print Alphabet Z Number Pattern
In this article we are going to see how to print alphabet ‘N’ number pattern.
Example-1 When rows value = 5 1 1 2 2 2 3 3 3 4 4 4 5 5
Example-2: When rows value=7 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7
Now, let’s see the actual program to print it.
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
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 N Number Pattern
import java.util.Scanner; class pattern { 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 right = 1, left = 1, dia = 2; //Iterates from 0 to (2 * rows) -1 for (numberOfRows = 0; numberOfRows < rows; numberOfRows++) { //Prints the left side System.out.print(left++ + " "); //Space for diagonal for (numberOfColumns = 0; numberOfColumns < 2 * numberOfRows; numberOfColumns++) System.out.print(" "); //Prints diagonal if (numberOfRows != 0 && numberOfRows != rows - 1) System.out.print(dia++); else System.out.print(" "); //Space for right values for (numberOfColumns = 0; numberOfColumns < 2 * (rows - numberOfRows - 1); numberOfColumns++) { System.out.print(" "); } // Prints the right side System.out.print(right++); // Prints a new line System.out.println(); } } }
Output: Rows : 5 1 1 2 2 2 3 3 3 4 4 4 5 5
C Code to Print Alphabet N 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 right = 1, left = 1, dia = 2; //Iterates from 0 to (2 * rows) -1 for (numberOfRows = 0; numberOfRows < rows; numberOfRows++) { //Prints the left side printf("%d ", left++); //Space for diagonal for (numberOfColumns = 0; numberOfColumns < 2 * numberOfRows; numberOfColumns++) printf(" "); //Prints diagonal if (numberOfRows != 0 && numberOfRows != rows - 1) printf("%d", dia++); else printf(" "); //Space for right values for (numberOfColumns = 0; numberOfColumns < 2 * (rows - numberOfRows - 1); numberOfColumns++) { printf(" "); } // Prints the right side printf("%d", right++); // Prints a new line printf("\n"); } return 0; }
Output: Rows : 5 1 1 2 2 2 3 3 3 4 4 4 5 5
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: