# Program for triangular pattern (mirror image around 0)

Given the value of n, print the pattern.

Examples :

```Input : n = 5
Output :
0
101
21012
3210123
432101234

Input : n = 7
Output :
0
101
21012
3210123
432101234
54321012345
6543210123456
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Below is the program to print the above pattern

## C++

 `// C++ Implementation to print the pattern ` `#include ` `using` `namespace` `std; ` ` `  `// Function definition ` `void` `print(``int` `n) ` `{ ` `    ``int` `var1 = 1, var2 = 1; ` ` `  `    ``// Outer for loop to keep ` `    ``// track of number of lines ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``// for loop to keep track ` `        ``// of spaces ` `        ``for` `(``int` `j = n - 1; j > i; j--) { ` `            ``cout << ``" "``; ` `        ``} ` ` `  `        ``// for loop to print the ` `        ``// digits in pattern ` `        ``for` `(``int` `k = 1; k <= var1; k++) { ` `            ``cout << ``abs``(k - var2); ` `        ``} ` ` `  `        ``var1 += 2; ` `        ``var2++; ` `        ``cout << ``"\n"``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``// taking size from the user ` `    ``int` `n = 5; ` ` `  `    ``// function calling ` `    ``print(n); ` `     `  `    ``return` `0; ` `} `

## Java

 `// Java Implementation to print the pattern ` ` `  `class` `GFG ` `{ ` `    ``// Function definition ` `    ``static` `void` `print(``int` `n) ` `    ``{ ` `        ``int` `var1 = ``1``, var2 = ``1``; ` `     `  `        ``// Outer for loop to keep ` `        ``// track of number of lines ` `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``{ ` `            ``// for loop to keep track ` `            ``// of spaces ` `            ``for` `(``int` `j = n - ``1``; j > i; j--)  ` `            ``{ ` `                ``System.out.print(``" "``); ` `            ``} ` `     `  `            ``// for loop to print the ` `            ``// digits in pattern ` `            ``for` `(``int` `k = ``1``; k <= var1; k++)  ` `            ``{ ` `                ``System.out.print(Math.abs(k - var2)); ` `            ``} ` `     `  `            ``var1 += ``2``; ` `            ``var2++; ` `            ``System.out.println(); ` `        ``} ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `        ``// taking size from the user ` `        ``int` `n = ``5``; ` `     `  `        ``// function calling ` `        ``print(n); ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python3

 `# Python Implementation to  ` `# print the pattern ` ` `  `# Function definition ` `def` `printt(n): ` ` `  `    ``var1 ``=` `1` `    ``var2 ``=` `1` `  `  `    ``# Outer for loop to keep ` `    ``# track of number of lines ` `    ``for` `i ``in` `range``(n): ` ` `  `        ``# for loop to keep track ` `        ``# of spaces ` `        ``for` `j ``in` `range``(n ``-` `1``,i,``-``1``): ` `            ``print``(``" "``, end ``=` `"") ` ` `  `        ``# for loop to print the ` `        ``# digits in pattern ` `        ``for` `k ``in` `range``(``1``, var1 ``+` `1``): ` `            ``print``(``abs``(k ``-` `var2), end``=``"") ` `         `  `        ``var1 ``+``=` `2` `        ``var2 ``+``=` `1` `        ``print``() ` ` `  `# Driver code ` `# taking size from the user ` `n ``=` `5` `  `  `# function calling ` `printt(n) ` ` `  `# This code is contributed ` `# by Anant Agarwal. `

## C#

 `//C# Implementation to print the pattern ` `using` `System; ` `class` `GFG ` `{ ` `    ``// Function definition ` `    ``static` `void` `print(``int` `n) ` `    ``{ ` `        ``int` `var1 = 1, var2 = 1; ` `     `  `        ``// Outer for loop to keep ` `        ``// track of number of lines ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{ ` `            ``// for loop to keep track ` `            ``// of spaces ` `            ``for` `(``int` `j = n - 1; j > i; j--)  ` `            ``{ ` `                ``Console.Write(``" "``); ` `            ``} ` `     `  `            ``// for loop to print the ` `            ``// digits in pattern ` `            ``for` `(``int` `k = 1; k <= var1; k++)  ` `            ``{ ` `            ``Console.Write(Math.Abs(k - var2)); ` `            ``} ` `     `  `            ``var1 += 2; ` `            ``var2++; ` `            ``Console.WriteLine(); ` `        ``} ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `        ``// taking size from the user ` `        ``int` `n = 5; ` `     `  `        ``// function calling ` `        ``print(n); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` ``\$i``; ``\$j``--)  ` `        ``{ ` `            ``echo` `" "``; ` `        ``} ` ` `  `        ``// for loop to print the ` `        ``// digits in pattern ` `        ``for` `(``\$k` `= 1; ``\$k` `<= ``\$var1``; ``\$k``++)  ` `        ``{ ` `            ``echo` `abs``(``\$k` `- ``\$var2``); ` `        ``} ` ` `  `        ``\$var1` `+= 2; ` `        ``\$var2``++; ` `        ``echo` `"\n"``; ` `    ``} ` `} ` ` `  `// Driver code ` `\$n` `= 5; ` `print1(``\$n``); ` ` `  `// This code is contributed by mits  ` `?> `

Output :

```    0
101
21012
3210123
432101234
```

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 : Mithun Kumar

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.