Thursday, October 23, 2025
HomeData Modelling & AIProgram to print numeric pattern

Program to print numeric pattern

Given the value of n i.e, the number of rows, print the following pattern.

Examples : 

Input : n = 4
Output :
1
5 2
8 6 3
10 9 7 4

Input : n = 6
Output :
1
7 2
12 8 3
16 13 9 4
19 17 14 10 5
21 20 18 15 11 6

Approach: The approach is to start printing the pattern from the end of each row. After the completion of the last column of each row, start from the second last column of the second row and so on. Below given is the implementation of this approach :

C++




// C++ program to print the pattern
#include <bits/stdc++.h>
using namespace std;
  
// Function to print pattern 
// for given value of n
void pattern(int n)
{
    int p, k = 1;
      
    // Outer "for" loop for number of rows
    for (int i = 1; i <= n; i++) {
          
        // set the value of "p" as "k".
        p = k;
  
        // Inner "for" loop for number of columns
        for (int j = 1; j <= i; j++) {
              
            // print the values
            cout << p << " ";
              
            // change in value of "p" for
            // every elements after the first 
            // element of each row .
            p = p - (n + j - i);
        }
        cout << endl;
  
        // value of "k" for first 
        // element of every row.
        k = k + 1 + n - i;
    }
}
  
// Driver Code
int main()
{
    int n = 5;
  
    // Function calling
    pattern(n);
  
    return 0;
}


Java




// Java program to print the given pattern
import java.util.*;
  
class GfG {
  
    // Function to print pattern for
    // given value of n
    static void pattern(int n)
    {
        int p, k = 1;
          
        // Outer "for" loop for number of rows
        for (int i = 1; i <= n; i++) {
              
            // set the value of "p" as "k".
            p = k;
  
            // Inner "for" loop for number of columns
            for (int j = 1; j <= i; j++) {
                  
                // print the values
                System.out.print(p);
                System.out.print(" ");
                  
                // change in value of "p" for
                // every elements after the 
                // first element of each row .
                p = p - (n + j - i);
            }
              
            // Print the next line
            System.out.println();
  
            // value of "k" for first 
            // element of every row.
            k = k + 1 + n - i;
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int n = 5;
  
        // Function calling
        pattern(n);
    }
}


Python 3




# Python 3 program to print the pattern
  
# Function to print pattern 
# for given value of n
def pattern(n):
  
    k = 1
      
    # Outer "for" loop for number of rows
    for i in range(1 , n+1):
          
        # set the value of "p" as "k".
        p = k
  
        # Inner "for" loop for number of columns
        for j in range(1 , i+1):
              
            # print the values
            print(p , end=" ")
              
            # change in value of "p" for
            # every elements after the first 
            # element of each row .
            p = p - (n + j - i)
          
        print("")
        # value of "k" for first 
        # element of every row.
        k = k + 1 + n - i
      
# Driver Code
n = 5
  
# Function calling
pattern(n)
  
# This code is contributed by Smitha


C#




// C# program to print the given pattern
using System;
  
class GfG {
  
    // Function to print pattern for
    // given value of n
    static void pattern(int n)
    {
        int p, k = 1;
          
        // Outer "for" loop for number
        // of rows
        for (int i = 1; i <= n; i++) {
              
            // set the value of "p" as "k".
            p = k;
  
            // Inner "for" loop for number
            // of columns
            for (int j = 1; j <= i; j++) {
                  
                // print the values
                Console.Write(p);
                Console.Write(" ");
                  
                // change in value of "p" for
                // every elements after the 
                // first element of each row .
                p = p - (n + j - i);
            }
              
            // Print the next line
            Console.WriteLine();
  
            // value of "k" for first 
            // element of every row.
            k = k + 1 + n - i;
        }
    }
  
    // Driver Code
    public static void Main()
    {
        int n = 5;
  
        // Function calling
        pattern(n);
    }
}
  
// This code is contributed by anuj_67.


PHP




<?php
// PHP program to print the pattern
  
// Function to print pattern for
// given value of n
function pattern($n)
{
    $p;
    $k = 1;
      
    // Outer "for" loop for number of rows
    for($i = 1; $i <= $n; $i++)
    {
        // set the value of "p" as "k".
        $p = $k;
          
        // Inner "for" loop for number of columns
        for($j = 1; $j <= $i; $j++)
        {
            // print the values
            echo $p;
            echo " ";
              
            // change in value of "p" for
            // every elements after the
            // first element of each row . 
            $p = $p - ($n + $j - $i); 
        }
        echo "\n";
          
        // value of "k" for first 
        // element of every row.
        $k = $k + 1 + $n - $i;
    }
}
  
// Driver Code
  
$n = 5;
  
// Function calling
pattern($n);
  
?>


Javascript




<script>
  
// JavaScript program to print the pattern
  
      // Function to print pattern
      // for given value of n
      function pattern(n) {
        var p, k = 1;
  
        // Outer "for" loop for number of rows
        for (var i = 1; i <= n; i++) {
          
          // set the value of "p" as "k".
          p = k;
  
          // Inner "for" loop for number of columns
          for (var j = 1; j <= i; j++) {
            
            // print the values
            document.write(p + "  ");
  
            // change in value of "p" for
            // every elements after the first
            // element of each row .
            p = p - (n + j - i);
          }
          document.write("<br>");
  
          // value of "k" for first
          // element of every row.
          k = k + 1 + n - i;
        }
      }
  
      // Driver Code
      var n = 5;
        
      // Function calling
      pattern(n);
        
</script>


Output

1 
6 2 
10 7 3 
13 11 8 4 
15 14 12 9 5 

Time Complexity: O(n2)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS