Program to print 'N' alphabet using the number pattern from 1 to n - GeeksforGeeks

15 Apr, 2021
Given an integer N, the task is to print the Alphabet N Pattern as given below: 
1           1
2 2         2
3   3       3
*    *      *
*      *    *
*        *  *
N           N
Examples:  
Input: N = 6
Output:
1           1
2  2        2
3    3      3
4      4    4
5        5  5
6           6

Input: N = 5
Output:
1         1
2  2      2
3    3    3
4      4  4
5         5
 
Approach: Except the first and the last row, every other row will follow the following:  
Print the first value as index + 1 where index is the index of the row.
Then print blank spaces 2 * index times.
Again print the value index + 1 as the diagonal element for the current row.
Then print the rest of the 2 * (N – index – 1) blank spaces followed by the ending element which is again index + 1.
Below is the implementation of the above approach:  
C++
C
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <iostream>using namespace std; // Function to print the desired Alphabet N Patternvoid Alphabet_N_Pattern(int N){     int index, side_index, size;     // Declaring the values of Right, Left and Diagonal values    int Right = 1, Left = 1, Diagonal = 2;     // Main Loop for the rows    for (index = 0; index < N; index++) {         // For the left Values        cout << Left++;         // Spaces for the diagonals        for (side_index = 0; side_index < 2 * (index); side_index++)            cout << " ";         // Condition for the diagonals        if (index != 0 && index != N - 1)            cout << Diagonal++;        else            cout << " ";         // Spaces for the Right Values        for (side_index = 0; side_index < 2 * (N - index - 1); side_index++)            cout << " ";         // For the right values        cout << Right++;         cout << endl;    }} // Driver Codeint main(int argc, char** argv){    // Size of the Pattern    int Size = 6;     // Calling the function to print the desired Pattern    Alphabet_N_Pattern(Size);}
// C implementation of the approach#include <stdio.h> // Function to print the desired Alphabet N Patternvoid Alphabet_N_Pattern(int N){     int index, side_index, size;     // Declaring the values of Right, Left and Diagonal values    int Right = 1, Left = 1, Diagonal = 2;     // Main Loop for the rows    for (index = 0; index < N; index++) {         // For the left Values        printf("%d", Left++);         // Spaces for the diagonals        for (side_index = 0; side_index < 2 * (index); side_index++)            printf(" ");         // Condition for the diagonals        if (index != 0 && index != N - 1)            printf("%d", Diagonal++);        else            printf(" ");         // Spaces for the Right Values        for (side_index = 0; side_index < 2 * (N - index - 1); side_index++)            printf(" ");         // For the right values        printf("%d", Right++);         printf("\n");    }} // Driver Codeint main(int argc, char** argv){    // Size of the Pattern    int Size = 6;     // Calling the function to print the desired Pattern    Alphabet_N_Pattern(Size);}
// Java implementation of the approachimport java.util.*;  class solution{ // Function to print the desired Alphabet N Patternstatic void Alphabet_N_Pattern(int N){     int index, side_index, size;     // Declaring the values of Right, Left and Diagonal values    int Right = 1, Left = 1, Diagonal = 2;     // Main Loop for the rows    for (index = 0; index < N; index++) {         // For the left Values        System.out.print(Left++);         // Spaces for the diagonals        for (side_index = 0; side_index < 2 * (index); side_index++)            System.out.print(" ");         // Condition for the diagonals        if (index != 0 && index != N - 1)            System.out.print(Diagonal++);        else            System.out.print(" ");         // Spaces for the Right Values        for (side_index = 0; side_index < 2 * (N - index - 1); side_index++)            System.out.print(" ");         // For the right values        System.out.print(Right++);         System.out.println();    }} // Driver Codepublic static void main(String args[]){    // Size of the Pattern    int Size = 6;     // Calling the function to print the desired Pattern    Alphabet_N_Pattern(Size);} } // This code is contributed by// Surendra_Gagwar
# Python 3 implementation of the approach # Function to print the desired# Alphabet N Patterndef Alphabet_N_Pattern(N):         # Declaring the values of Right, Left    # and Diagonal values    Right = 1    Left = 1    Diagonal = 2     # Main Loop for the rows    for index in range(N):                 # For the left Values        print(Left, end = "")        Left += 1         # Spaces for the diagonals        for side_index in range(0, 2 * (index), 1):            print(" ", end = "")         # Condition for the diagonals        if (index != 0 and index != N - 1):            print(Diagonal, end = "")            Diagonal += 1        else:            print(" ", end = "")         # Spaces for the Right Values        for side_index in range(0, 2 * (N - index - 1), 1):            print(" ", end = "")         # For the right values        print(Right, end = "")        Right += 1                 print("\n", end = "")     # Driver Codeif __name__ == '__main__':         # Size of the Pattern    Size = 6     # Calling the function to print    # the desired Pattern    Alphabet_N_Pattern(Size) # This code is contributed by# Sanjit_Prasad
// C# implementation of the approachusing System; class GFG{     // Function to print the desired Alphabet N Patternpublic static void Alphabet_N_Pattern(int N){     int index, side_index;         // Declaring the values of Right,    // Left and Diagonal values    int Right = 1, Left = 1, Diagonal = 2;         // Main Loop for the rows    for (index = 0; index < N; index++)    {         // For the left Values        Console.Write(Left++);         // Spaces for the diagonals        for (side_index = 0;             side_index < 2 * (index); side_index++)            Console.Write(" ");         // Condition for the diagonals        if (index != 0 && index != N - 1)            Console.Write(Diagonal++);        else            Console.Write(" ");                 // Spaces for the Right Values        for (side_index = 0;             side_index < 2 * (N - index - 1);             side_index++)            Console.Write(" ");         // For the right values        Console.Write(Right++);         Console.Write("\n");    }} // Driver Codestatic void Main(){    // Size of the Pattern    int Size = 6;     // Calling the function to print    // the desired Pattern    Alphabet_N_Pattern(Size);}} // This code is contributed by DrRoot_
<?php// PHP implementation of the approach // Function to print the desired// Alphabet N Patternfunction Alphabet_N_Pattern($N){    $index;    $side_index;    $size;     // Declaring the values of Right,    // Left and Diagonal values    $Right = 1;    $Left = 1;    $Diagonal = 2;     // Main Loop for the rows    for ($index = 0; $index < $N; $index++)    {         // For the left Values        echo $Left++;         // Spaces for the diagonals        for ($side_index = 0;             $side_index < 2 * ($index);             $side_index++)            echo " ";         // Condition for the diagonals        if ($index != 0 && $index != $N - 1)            echo $Diagonal++;        else            echo " ";         // Spaces for the Right Values        for ($side_index = 0;             $side_index < 2 * ($N - $index - 1);             $side_index++)            echo " ";         // For the right values        echo $Right++;         echo "\n";    }} // Driver Code // Size of the Pattern$Size = 6; // Calling the function to// print the desired PatternAlphabet_N_Pattern($Size); // This code is contributed by ajit?>
<script>       // JavaScript implementation of the approach       // Function to print the desired      // Alphabet N Pattern      function Alphabet_N_Pattern(N) {        var index, side_index, size;         // Declaring the values of Right,        // Left and Diagonal values        var Right = 1,          Left = 1,          Diagonal = 2;         // Main Loop for the rows        for (index = 0; index < N; index++) {          // For the left Values          document.write(Left++);           // Spaces for the diagonals          for (side_index = 0;          side_index < 2 * index; side_index++)            document.write("  ");           // Condition for the diagonals          if (index != 0 && index != N - 1)          document.write(Diagonal++);          else          document.write("  ");           // Spaces for the Right Values          for (side_index = 0;          side_index < 2 * (N - index - 1); side_index++)            document.write("  ");           // For the right values          document.write(Right++);          document.write("<br>");        }      }       // Driver Code      // Size of the Pattern      var Size = 6;      // Calling the function to print      // the desired Pattern      Alphabet_N_Pattern(Size);       </script>
1           1
2  2        2
3    3      3
4      4    4
5        5  5
6           6
 
jit_t
SURENDRA_GANGWAR
DrRoot_
Sanjit_Prasad
rdtank
pattern-printing
Mathematical
School Programming
Mathematical
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Print all possible combinations of r elements in a given array of size n
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects