Program to print pattern

Given the value of n, print the following pattern.

Examples :

Input : n = 4
Output :
   A1
  AB12
 ABC123
ABCD1234

Input : n = 7
Output :
      A1
     AB12
    ABC123
   ABCD1234
  ABCDE12345
 ABCDEF123456
ABCDEFG1234567

Below is the implementation to print the above pattern :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print given pattern
#include <bits/stdc++.h>
using namespace std;
  
// Function to print pattern for
// given value of n
void pattern(int n)
{
    // Outer "for" loop for number of rows
    for (int i = 1; i <= n; i++) {
        int k = 'A';
        int m = 1;
  
        // Inner "for" loop for number of columns
        for (int j = 1; j <= (2 * n); j++) {
  
            // Logical execution i.e, condition
            if (j >= n + 1 - i && (j <= n + i)) {
                if (j <= n) {
  
                    // Print the alphabets
                    cout << char(k);
                    k++;
                }
                else {
  
                    // Print the numbers
                    cout << m;
                    m++;
                }
            }
            else
                cout << " ";
        }
  
        // Print the next line
        cout << endl;
    }
}
  
// Driver Code
int main()
{
    int n = 7;
  
    // Function calling
    pattern(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// java program to print given pattern
import java.util.*;
  
class GFG {
      
    // Function to print pattern for
    // given value of n
    static void pattern(int n)
    {
          
        // Outer "for" loop for number of rows
        for (int i = 1; i <= n; i++) {
            int k = 'A';
            int m = 1;
      
            // Inner "for" loop for number of
            // columns
            for (int j = 1; j <= (2 * n); j++) {
      
                // Logical execution i.e, condition
                if (j >= n + 1 - i && (j <= n + i))
                {
                    if (j <= n) {
      
                        // Print the alphabets
                        System.out.print((char)k);
                        k++;
                    }
                    else {
      
                        // Print the numbers
                        System.out.print(m);
                        m++;
                    }
                }
                else
                        System.out.print(" ");
            }
      
        // Print the next line
        System.out.println();
        }
    }
      
    // Driver Code
    public static void main(String[] args)
    {
        int n = 7;
      
        // Function calling
        pattern(n);
    }
}
  
// This code is contributed by Anuj_67

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to 
# print given pattern
  
# Function to print
# pattern for
# given value of n
def pattern(n):
      
    # Outer "for" loop 
    # for number of rows
    i = 1
    while i <= n:
      
        k = 65;
        m = 1;
  
        # Inner "for" loop for
        # number of columns
        j = 1
        while j <= (2 * n):
  
            # Logical execution 
            # i.e, condition
            if (j >= n + 1 - i) and (j <= n + i): 
                if (j <= n): 
  
                    # Print the alphabets
                    print(chr(k)),
                    k += 1
                else:
  
                    # Print the numbers
                    print(m),
                    m += 1
              
            else:
                print(" "),
            j += 1
        i += 1
  
        # Print the next 
        # line 
        print("\n")
      
  
# Driver Code
def main():
    n = 7
      
    # Function calling
    pattern(n)
  
if __name__=="__main__":
    main() 
      
# This code is contributed
# prabhat kumar singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print given pattern
using System;
  
class GFG {
      
    // Function to print pattern for
    // given value of n
    static void pattern(int n)
    {
          
        // Outer "for" loop for number of rows
        for (int i = 1; i <= n; i++) {
            int k = 'A';
            int m = 1;
      
            // Inner "for" loop for number of
            // columns
            for (int j = 1; j <= (2 * n); j++) {
      
                // Logical execution i.e, condition
                if (j >= n + 1 - i && (j <= n + i))
                {
                    if (j <= n) {
      
                        // Print the alphabets
                        Console.Write((char)k);
                        k++;
                    }
                    else {
      
                        // Print the numbers
                        Console.Write(m);
                        m++;
                    }
                }
                else
                        Console.Write(" ");
            }
      
        // Print the next line
        Console.WriteLine();
        }
    }
      
    // Driver Code
    public static void Main()
    {
        int n = 7;
      
        // Function calling
        pattern(n);
    }
}
  
// This code is contributed by Anuj_67

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print given pattern
  
// Function to print pattern for
// given value of n
function pattern($n)
{
      
    // Outer "for" loop for
    // number of rows
    for($i = 1; $i <= $n; $i++)
    {
        $k = 'A';
        $m = 1;
  
        // Inner "for" loop for
        // number of columns
        for($j = 1; $j <= (2 * $n); $j++)
        {
  
            // Logical execution i.e, condition
            if ($j >= $n + 1 - $i && 
                    ($j <= $n + $i)) 
            {
                if ($j <= $n)
                {
                    // Print the alphabets
                    echo $k;
                    $k++;
                }
                else 
                {
                    // Print the numbers
                    echo $m;
                    $m++;
                }
            }
            else
                echo " ";
        }
  
        // Print the next line
        echo "\n";
    }
}
  
    // Driver Code
    $n = 7;
  
    // Function calling
    pattern($n);
  
// This code is contributed by Anuj_67
?>

chevron_right


Output :

      A1      
     AB12     
    ABC123    
   ABCD1234   
  ABCDE12345  
 ABCDEF123456 
ABCDEFG1234567


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m, prabhat kumar singh