📜  用星形图案打印数字的程序

📅  最后修改于: 2022-05-13 01:57:59.524000             🧑  作者: Mango

用星形图案打印数字的程序

我们必须打印下面示例中给出的模式。
例子 :

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


输出:

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