Program to print number with star pattern

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 patern 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 patern 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 patern    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 patern 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



Output:

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

My Personal Notes arrow_drop_up Hello everyone, I am Bishal KUMAR Dubey and most importantly an idea creator I just love Programming languages and love to know new concepts everyday,every minute,every second Here to help Other GEEKS

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.