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

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// php program to print
// above pattern
  
function 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 = 1; $i <= $n; $i++)
    {
        for ($j = 1; $j <= $sp; $j++)
        {
            echo " ";
        }
          
        $count = 1;
        for ($k = 1; $k <= $st; $k++)
        {
            if ($k % 2 == 0)
                echo "*";
            else
                echo $count++;
        }
  
        echo "\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
$n = 5;
display($n);
  
// This code is contributed by mits 
?>

chevron_right



Output:

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



My Personal Notes arrow_drop_up


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.