Program to print a pattern of numbers - GeeksforGeeks

28 Jun, 2021
The idea of pattern based programs is to understand the concept of nesting of for loops and how and where to place the alphabets / numbers / stars to make the desired pattern.Write to program to print the pattern of numbers in the following manner using for loop  
    1
   232
  34543
 4567654
567898765
In almost all types of pattern programs, two things that you must take care:  
No. of linesIf the pattern is increasing or decreasing per line?
No. of lines
If the pattern is increasing or decreasing per line?
Implementation  
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to illustrate the above// given pattern of numbers.#include<bits/stdc++.h>using namespace std; int main(){    int n = 5, i, j, num = 1, gap;    gap = n - 1;     for ( j = 1 ; j <= n ; j++ )    {        num = j;        for ( i = 1 ; i <= gap ; i++ )            cout << " ";         gap --;        for ( i = 1 ; i <= j ; i++ )        {            cout << num;            num++;        }        num--;        num--;        for ( i = 1 ; i < j ; i++)        {            cout << num;            num--;        }        cout << "\n";    }    return 0;} //This code is contributed by Shivi_Aggarwal
// C program to illustrate the above// given pattern of numbers.#include<stdio.h>  int main(){      int n = 5, i, j, num = 1, gap;        gap = n - 1;        for ( j = 1 ; j <= n ; j++ )      {          num = j;            for ( i = 1 ; i <= gap ; i++ )              printf(" ");            gap --;            for ( i = 1 ; i <= j ; i++ )          {              printf("%d", num);              num++;          }          num--;          num--;          for ( i = 1 ; i < j ; i++)          {              printf("%d", num);              num--;          }          printf("\n");        }        return 0;}
// Java Program to illustrate the// above given pattern of numbersimport java.io.*; class GFG {         public static void main(String args[])    {                 int n = 5, i, j, num = 1, gap;         gap = n - 1;         for ( j = 1 ; j <= n ; j++ )        {        num = j;                 for ( i = 1 ; i <= gap ; i++ )            System.out.print(" ");                 gap --;                 for ( i = 1 ; i <= j ; i++ )        {            System.out.print(num);            num++;        }        num--;        num--;        for ( i = 1 ; i < j ; i++)        {            System.out.print(num);            num--;        }        System.out.println();        }    }}  // This code is contributed// by Nikita tiwari.
# Python Program to illustrate the# above given pattern of numbers. n = 5num = 1gap = n - 1for j in range(1, n + 1) :    num = j    for i in range(1, gap + 1) :        print(" ", end="")    gap = gap - 1             for i in range(1, j + 1) :        print(num, end="")        num = num + 1         num = num - 2    for i in range(1, j) :        print(num, end="")        num = num - 1         print() # This code is contributed# by Nikita tiwari.
// C# Program to illustrate the// above given pattern of numbersusing System; class GFG {     public static void Main()    {         int n = 5, i, j, num = 1, gap;         gap = n - 1;         for (j = 1; j <= n; j++) {            num = j;             for (i = 1; i <= gap; i++)                Console.Write(" ");             gap--;             for (i = 1; i <= j; i++) {                Console.Write(num);                num++;            }            num--;            num--;            for (i = 1; i < j; i++) {                Console.Write(num);                num--;            }            Console.WriteLine();        }    }} // This code is contributed// by vt_m.
<?php//php program to illustrate the above// given pattern of numbers. $n = 5;$num = 1;$gap = $n - 1; for ($j = 1; $j <= $n; $j++){    $num = $j;     for ($i = 1; $i <= $gap; $i++)        printf(" ");     $gap --;     for ($i = 1; $i <= $j; $i++)    {        printf($num);        $num++;    }    $num--;    $num--;    for ($i = 1; $i < $j; $i++)    {        printf($num);        $num--;    }    printf("\n"); } // This code is contributed by mits?>
<script>      // JavaScript program to illustrate the above      // given pattern of numbers.      var n = 5,        i,        j,        num = 1,        gap;      gap = n - 1;       for (j = 1; j <= n; j++) {        num = j;        for (i = 1; i <= gap; i++) document.write("  ");         gap--;        for (i = 1; i <= j; i++) {          document.write(num);          num++;        }        num--;        num--;        for (i = 1; i < j; i++) {          document.write(num);          num--;        }        document.write("<br>");      }             // This code is contributed by rdtank.    </script>
Output:  
    1
   232
  34543
 4567654
567898765
Program for Pyramid PatternPlease write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
Mithun Kumar
Shivi_Aggarwal
rdtank
pattern-printing
C Language
C++
School Programming
pattern-printing
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multidimensional Arrays in C / C++
rand() and srand() in C/C++
fork() in C
Left Shift and Right Shift Operators in C/C++
Core Dump (Segmentation fault) in C/C++
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects