# Program to print number with star pattern

• Difficulty Level : Hard
• Last Updated : 04 Jun, 2022

We have to print the pattern as given in the below example.
Examples :

```Input : 5
Output :
1
1*2
1*2*3
1*2
1

Input : 9
Output :
1
1*2
1*2*3
1*2*3*4
1*2*3*4*5
1*2*3*4
1*2*3
1*2
1```

## C++

 `#include ``using` `namespace` `std;` `// C++ program to print above pattern``void` `display(``int` `n)``{` `    ``// 'sp' used for space and 'st' used for star``    ``int` `sp = n / 2, st = 1;` `    ``// Outer for loop prints number of lines``    ``for` `(``int` `i = 1; i <= n; i++) {``        ``for` `(``int` `j = 1; j <= sp; j++) {``            ``cout << ``" "``;``        ``}``        ` `        ``int` `count = 1;``        ``for` `(``int` `k = 1; k <= st; k++) {``            ``if` `(k % 2 == 0)``                ``cout << ``"*"``;``            ``else``                ``cout << count++;``        ``}` `        ``cout << ``"\n"``;` `        ``if` `(i <= n / 2) {``            ` `            ``// Before reaching to half after``            ``// every line space is decreased``            ``// by 1 and star is increased by 2``            ``sp = sp - 1;``            ``st = st + 2;``        ``}``        ``else` `{``            ` `            ``// After reaching to half``            ``// space is increased by 1``            ``// and star is decreased by 2``            ``sp = sp + 1;``            ``st = st - 2;``        ``}``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `n = 5;``    ``display(n);``    ``return` `0;``}` `// This code is contributed by vt_m`

## Java

 `// Java program to print above pattern``import` `java.util.Scanner;``class` `Pattern``{``    ``void` `display(``int` `n)``    ``{``    ` `        ``// 'sp' used for space and 'st' used for star``        ``int` `sp = n / ``2``, st = ``1``;``        ` `        ``// Outer for loop prints number of lines``        ``for` `(``int` `i = ``1``; i <= n; i++)``        ``{``            ``for` `(``int` `j = ``1``; j <= sp; j++)``            ``{``                ``System.out.print(``" "``);``            ``}``            ``int` `count = ``1``;``            ``for` `(``int` `k = ``1``; k <= st; k++)``            ``{``                ``if` `(k % ``2` `== ``0``)``                    ``System.out.print(``"*"``);``                ``else``                    ``System.out.print(count++);``            ``}``            ` `            ``System.out.println();``            ` `            ``if` `(i <= n / ``2``)``            ``{``                ``// Before reaching to half after``                ``// every line space is decreased``                ``// by 1 and star is increased by 2``                ``sp = sp - ``1``;``                ``st = st + ``2``;``            ``}``            ``else``            ``{``                ``// After reaching to half``                ``// space is increased by 1``                ``// and star is decreased by 2``                ``sp = sp + ``1``;``                ``st = st - ``2``;``            ``}``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `n = ``5``;``        ``Pattern p = ``new` `Pattern();``        ``p.display(n);``    ``}``}`

## Python3

 `# Python3 program to print above pattern` `def` `display(n):` `    ``# 'sp' used for space and``    ``# 'st' used for star``    ``sp ``=` `n ``/``/` `2``    ``st ``=` `1` `    ``# Outer for loop prints number``    ``# of lines``    ``for` `i ``in` `range``(``1``, n ``+` `1``):``        ``for` `j ``in` `range``(``1``, sp ``+` `1``):``            ``print``(end ``=` `" "``)` `        ``count ``=` `1``        ``for` `k ``in` `range``(``1``, st ``+` `1``):``            ``if` `(k ``%` `2` `=``=` `0``):``                ``print``(``"*"``, end ``=` `"")``            ``else``:``                ``print``(count, end ``=` `"")``                ``count ``+``=` `1``        ` `        ``print``()` `        ``if` `(i <``=` `n ``/``/` `2``):``            ` `            ``# Before reaching to half after``            ``# every line space is decreased``            ``# by 1 and star is increased by 2``            ``sp ``=` `sp ``-` `1``            ``st ``=` `st ``+` `2``        ` `        ``else``:``            ` `            ``# After reaching to half``            ``# space is increased by 1``            ``# and star is decreased by 2``            ``sp ``=` `sp ``+` `1``            ``st ``=` `st ``-` `2``        ` `# Driver Code``n ``=` `5``display(n)` `# This code is contributed by``# Mohit kumar 29`

## C#

 `// C# program to print above pattern``using` `System;``class` `Pattern``{``    ``void` `display(``int` `n)``    ``{``    ` `        ``// 'sp' used for space and 'st' used for star``        ``int` `sp = n / 2, st = 1;``        ` `        ``// Outer for loop prints number of lines``        ``for` `(``int` `i = 1; i <= n; i++)``        ``{``            ``for` `(``int` `j = 1; j <= sp; j++)``            ``{``                ``Console.Write(``" "``);``            ``}``            ``int` `count = 1;``            ``for` `(``int` `k = 1; k <= st; k++)``            ``{``                ``if` `(k % 2 == 0)``                    ``Console.Write(``"*"``);``                ``else``                    ``Console.Write(count++);``            ``}``            ` `                    ``Console.WriteLine();``            ` `            ``if` `(i <= n / 2)``            ``{``                ``// Before reaching to half after``                ``// every line space is decreased``                ``// by 1 and star is increased by 2``                ``sp = sp - 1;``                ``st = st + 2;``            ``}``            ``else``            ``{``                ``// After reaching to half``                ``// space is increased by 1``                ``// and star is decreased by 2``                ``sp = sp + 1;``                ``st = st - 2;``            ``}``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int` `n = 5;``        ``Pattern p = ``new` `Pattern();``        ``p.display(n);``    ``}``}``//This code is contributed by vt_m.`

## PHP

 ``

## Javascript

 ``

Output:

```  1
1*2
1*2*3
1*2
1```

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

My Personal Notes arrow_drop_up