Program to print the pattern 1020304017018019020 **50607014015016 ****809012013 ******10011... - GeeksforGeeks

21 May, 2021
Given an integer N, the task is to print the pattern below for the given value of N.  
For N = 5, below is the given pattern:  
Examples:  
Input: N = 4 Output: 1020304017018019020 **50607014015016 ****809012013 ******10011
Input: N = 3 Output: 10203010011012 **4050809 ****607 
Approach: The idea to understand the logic behind the given pattern is stated below:
By looking closely, we see that by replacing in-between zeroes with spaces, the pattern can be seen more clearly. The pattern is further divided into three different patterns.
Case 1: Asterisk (*) character pattern follows a sequence from 0 and adds two more asterisks in each row, where the row is equal to N.Case 2: In this part, the pattern is very simple to understand. i.e the number of columns and rows will be equal to N and follows a sequence like 1, 2, 3, 4, 5...Case 3: Follow-up or bottom-up sequence is the interesting part where the numbers are represented from bottom to top.
Case 1: Asterisk (*) character pattern follows a sequence from 0 and adds two more asterisks in each row, where the row is equal to N.
Case 2: In this part, the pattern is very simple to understand. i.e the number of columns and rows will be equal to N and follows a sequence like 1, 2, 3, 4, 5...
Case 3: Follow-up or bottom-up sequence is the interesting part where the numbers are represented from bottom to top.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation to print// the given pattern#include <bits/stdc++.h>using namespace std; // Function to find the sum of// N integers from 1 to Nint sum(int n){    return n * (n - 1) / 2;} // Function to print the given// patternvoid BSpattern(int N){    int Val = 0, Pthree = 0,        cnt = 0, initial;     string s = "**";     // Iterate over [0, N - 1]    for (int i = 0; i < N; i++) {        cnt = 0;         // Sub-Pattern - 1        if (i > 0) {            cout << s;            s += "**";        }         // Sub-Pattern - 2        for (int j = i; j < N; j++) {             // Count the number of element            // in rows and sub-pattern 2 and 3            // will have same rows            if (i > 0) {                cnt++;            }            // Increment Val to print the            // series 1, 2, 3, 4, 5 ...            cout << ++Val;            cout << 0;        }         // To get the first element of sub        // pattern 3 find the sum of first N-1        // elements first N-1 elements in row1        // previous of Sub-Pattern 2         // Finally, add the (N-1)th element        // i.e., 5 and increment it by 1        if (i == 0) {            int Sumbeforelast = sum(Val) * 2;            Pthree = Val + Sumbeforelast + 1;            initial = Pthree;        }         // Initial is used to give the initial        // value of the row in Sub-Pattern 3        initial = initial - cnt;         Pthree = initial;         // Sub-Pattern 3        for (int k = i; k < N; k++) {             cout << Pthree++;             // Skip printing zero at the last            if (k != N - 1) {                cout << 0;            }        }         cout << "\n";    }} // Driver Codeint main(){    // Given N    int N = 5;     // Function Call    BSpattern(N);    return 0;}
// Java implementation to print// the given patternimport java.util.*; class GFG{ // Function to find the sum of// N integers from 1 to Nstatic int sum(int n){    return n * (n - 1) / 2;} // Function to print the given// patternstatic void BSpattern(int N){    int Val = 0, Pthree = 0,        cnt = 0, initial = -1;     String s = "**";     // Iterate over [0, N - 1]    for(int i = 0; i < N; i++)    {        cnt = 0;                 // Sub-Pattern - 1        if (i > 0)        {            System.out.print(s);            s += "**";        }         // Sub-Pattern - 2        for(int j = i; j < N; j++)        {                         // Count the number of element            // in rows and sub-pattern 2            // and 3 will have same rows            if (i > 0)            {                cnt++;            }                         // Increment Val to print the            // series 1, 2, 3, 4, 5 ...            System.out.print(++Val);            System.out.print("0");        }         // To get the first element of sub        // pattern 3 find the sum of first N-1        // elements first N-1 elements in row1        // previous of Sub-Pattern 2         // Finally, add the (N-1)th element        // i.e., 5 and increment it by 1        if (i == 0)        {            int Sumbeforelast = sum(Val) * 2;            Pthree = Val + Sumbeforelast + 1;            initial = Pthree;        }         // Initial is used to give the initial        // value of the row in Sub-Pattern 3        initial = initial - cnt;         Pthree = initial;         // Sub-Pattern 3        for(int k = i; k < N; k++)        {            System.out.print(Pthree++);             // Skip printing zero at the last            if (k != N - 1)            {                System.out.print("0");            }        }        System.out.println();    }} // Driver codepublic static void main(String[] args){         // Given N    int N = 5;         // Function call    BSpattern(N);}} // This code is contributed by offbeat
# Python3 implementation to print# the given pattern # Function to find the sum of# N integers from 1 to Ndef sum(n):         return n * (n - 1) // 2  # Function to print the given# patterndef BSpattern(N):         Val = 0    Pthree = 0,    cnt = 0     initial = -1    s = "**"      # Iterate over [0, N - 1]    for i in range(N):        cnt = 0          # Sub-Pattern - 1        if (i > 0):            print(s, end = "")            s += "**"                  # Sub-Pattern - 2        for j in range(i, N):                         # Count the number of element            # in rows and sub-pattern 2 and 3            # will have same rows            if (i > 0):                cnt += 1                          # Increment Val to print the            # series 1, 2, 3, 4, 5 ...            Val += 1            print(Val, end = "")            print(0, end = "")                  # To get the first element of sub        # pattern 3 find the sum of first N-1        # elements first N-1 elements in row1        # previous of Sub-Pattern 2          # Finally, add the (N-1)th element        # i.e., 5 and increment it by 1        if (i == 0):            Sumbeforelast = sum(Val) * 2            Pthree = Val + Sumbeforelast + 1            initial = Pthree                  # Initial is used to give the initial        # value of the row in Sub-Pattern 3        initial = initial - cnt          Pthree = initial          # Sub-Pattern 3        for k in range(i, N):            print(Pthree, end = "")            Pthree += 1              # Skip printing zero at the last            if (k != N - 1):                print(0, end = "")                         print()         # Driver Code # Given NN = 5  # Function callBSpattern(N) # This code is contributed by sanjoy_62
// C# implementation to print// the given patternusing System;class GFG{  // Function to find the sum of// N integers from 1 to Nstatic int sum(int n){    return n * (n - 1) / 2;}  // Function to print the given// patternstatic void BSpattern(int N){    int Val = 0, Pthree = 0,        cnt = 0, initial = -1;      String s = "**";      // Iterate over [0, N - 1]    for(int i = 0; i < N; i++)    {        cnt = 0;                  // Sub-Pattern - 1        if (i > 0)        {            Console.Write(s);            s += "**";        }          // Sub-Pattern - 2        for(int j = i; j < N; j++)        {                          // Count the number of element            // in rows and sub-pattern 2            // and 3 will have same rows            if (i > 0)            {                cnt++;            }                          // Increment Val to print the            // series 1, 2, 3, 4, 5 ...            Console.Write(++Val);            Console.Write("0");        }          // To get the first element of sub        // pattern 3 find the sum of first N-1        // elements first N-1 elements in row1        // previous of Sub-Pattern 2          // Finally, add the (N-1)th element        // i.e., 5 and increment it by 1        if (i == 0)        {            int Sumbeforelast = sum(Val) * 2;            Pthree = Val + Sumbeforelast + 1;            initial = Pthree;        }          // Initial is used to give the initial        // value of the row in Sub-Pattern 3        initial = initial - cnt;          Pthree = initial;          // Sub-Pattern 3        for(int k = i; k < N; k++)        {            Console.Write(Pthree++);              // Skip printing zero at the last            if (k != N - 1)            {                Console.Write("0");            }        }        Console.WriteLine();    }}  // Driver codepublic static void Main(String[] args){          // Given N    int N = 5;          // Function call    BSpattern(N);}}  // This code is contributed by shikhasingrajput
<script>//Javascript implementation to print// the given pattern // Function to find the sum of// N integers from 1 to Nfunction sum( n){    return n * parseInt((n - 1) / 2);} // Function to print the given// patternfunction BSpattern( N){    var Val = 0, Pthree = 0,        cnt = 0, initial;     var s = "**";     // Iterate over [0, N - 1]    for (var i = 0; i < N; i++) {        cnt = 0;         // Sub-Pattern - 1        if (i > 0) {            document.write( s);            s += "**";        }         // Sub-Pattern - 2        for (var j = i; j < N; j++) {             // Count the number of element            // in rows and sub-pattern 2 and 3            // will have same rows            if (i > 0) {                cnt++;            }            // Increment Val to print the            // series 1, 2, 3, 4, 5 ...            document.write( ++Val);            document.write( 0);        }         // To get the first element of sub        // pattern 3 find the sum of first N-1        // elements first N-1 elements in row1        // previous of Sub-Pattern 2         // Finally, add the (N-1)th element        // i.e., 5 and increment it by 1        if (i == 0) {            var Sumbeforelast = sum(Val) * 2;            Pthree = Val + Sumbeforelast + 1;            initial = Pthree;        }         // Initial is used to give the initial        // value of the row in Sub-Pattern 3        initial = initial - cnt;         Pthree = initial;         // Sub-Pattern 3        for (var k = i; k < N; k++) {             document.write(Pthree++);             // Skip printing zero at the last            if (k != N - 1) {                document.write( 0);            }        }         document.write( "<br>");    }} // Given Nvar N = 5;// Function CallBSpattern(N); // This code is contributed by SoumikMondal</script>
102030405026027028029030
**6070809022023024025
****10011012019020021
******13014017018
********15016
 
Time Complexity: O(N2) Auxiliary Space: O(1) 
offbeat
shikhasingrajput
sanjoy_62
nidhi_biet
SoumikMondal
interview-preparation
Juspay
pattern-printing
Pattern Searching
School Programming
pattern-printing
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Boyer Moore Algorithm for Pattern Searching
Check if an URL is valid or not using Regular Expression
Search a Word in a 2D Grid of characters
How to check if string contains only digits in Java
Check if a string contains uppercase, lowercase, special characters and numeric values
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects