Open In App

# Program to Print a Pattern of Numbers

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:

1. No. of lines
2. If the pattern is increasing or decreasing per line?

Implementation

## C++

 `// C++ program to illustrate the above``// given pattern of numbers.``#include``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

 `// C program to illustrate the above``// given pattern of numbers.``#include`` ` `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

 `// Java Program to illustrate the``// above given pattern of numbers``import` `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.`

## Python3

 `# Python Program to illustrate the``# above given pattern of numbers.` `n ``=` `5``num ``=` `1``gap ``=` `n ``-` `1``for` `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#

 `// C# Program to illustrate the``// above given pattern of numbers``using` `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

 ``

## Javascript

 ``

Output:

```    1
232
34543
4567654
567898765```

Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Program for Pyramid Pattern

Write a program to print the triangle pyramid with numbers

Example

input n=10

Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10

Approach: using nested loops

This program prints a pyramid pattern of numbers where each row contains numbers from 1 to the row number.

Algorithm

1. Take the input for the number of rows.
2. Using two loops, the first loop is used to iterate over the rows, and the second loop is used to print the numbers in each row.
3. The first inner loop prints the spaces for each row, where the number of spaces is equal to the number of rows minus the current row number.
4. The second inner loop prints the numbers for each row, where the number of numbers is equal to the current row number.
5. The outer loop is used to move to the next row.

## Java

 `public` `class` `Main {``    ``public` `static` `void` `main(String[] args) {``        ``int` `n = ``10``;``        ``for` `(``int` `i = ``1``; i <= n; i++) {``            ``for` `(``int` `j = ``1``; j <= n - i; j++) {``                ``System.out.print(``" "``);``            ``}``            ``for` `(``int` `j = ``1``; j <= i; j++) {``                ``System.out.print(j + ``" "``);``            ``}``            ``System.out.println();``        ``}``    ``}``}`

## Python3

 `n``=``10``for` `i ``in` `range``(``1``,n``+``1``):``  ``print``(``" "``*``(n``-``i),end``=``"")``  ``for` `j ``in` `range``(``1``,i``+``1``):``    ``print``(j,end``=``" "``)``  ``print``()`

## Javascript

 `// Javascript code addition` `const n = 10;``for` `(let i = 1; i <= n; i++) {` `  ``for` `(let j = 1; j <= n - i; j++) {``    ``process.stdout.write(``' '``);``  ``}``  ``for` `(let j = 1; j <= i; j++) {``    ``process.stdout.write(j + ``' '``);``  ``}``  ``process.stdout.write(``"\n"``);``}` `// The code is contributed by Arushi Goel.`

Output

```         1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10 ```

Time complexity: O(n^2), where n is the number of rows. This is because we have two nested loops, and both loops iterate n times.

Auxiliary Space: O(1), as we are not using any additional data structures and are only printing the pattern on the console.