📜  程序打印风筝图案

📅  最后修改于: 2021-05-28 03:15:03             🧑  作者: Mango

给定的任务是使用’$’绘制风筝图案
例子:

Output:
       $
      $$$
     $$$$$
    $$$$$$$
   $$$$$$$$$
    $$$$$$$
     $$$$$
      $$$
       $
      $$$
     $$$$$

下面是实现上述问题的代码:
程序:

C++
// C++ Program to print Kite Pattern
#include 
#include 
 
using namespace std;
int main()
{
 
    int i, j, k, sp, space = 4;
    char prt = '$';
 
    for (i = 1; i <= 5; i++)
    {
 
        // For printing the space
        for (sp = space; sp >= 1; sp--)
        {
            cout << " ";
        }
 
        // For printing the $
        for (j = 1; j <= i; j++)
        {
            cout << prt;
        }
 
        for (k = 1; k <= (i - 1); k++)
        {
            if (i == 1)
            {
                continue;
            }
 
            cout << prt;
        }
        cout << "\n";
        space--;
    }
 
    space = 1;
 
    for (i = 4; i >= 1; i--)
    {
        for (sp = space; sp >= 1; sp--)
        {
            cout << " ";
        }
 
        for (j = 1; j <= i; j++)
        {
            cout << prt;
        }
 
        for (k = 1; k <= (i - 1); k++)
        {
            cout << prt;
        }
 
        space++;
        cout << "\n";
    }
 
    space = 3;
 
    for (i = 2; i <= 5; i++)
    {
        if ((i % 2) != 0)
        {
            for (sp = space; sp >= 1; sp--)
            {
                cout << " ";
            }
 
            for (j = 1; j <= i; j++)
            {
                cout << prt;
            }
        }
 
        if ((i % 2) != 0)
        {
            cout << "\n";
            space--;
        }
    }
 
    return 0;
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


C
// C Program to print Kite Pattern
#include 
#include 
 
int main()
{
 
    int i, j, k, sp, space = 4;
    char prt = '$';
 
    for (i = 1; i <= 5; i++) {
 
        // For printing the space
        for (sp = space; sp >= 1; sp--) {
            printf(" ");
        }
 
        // For printing the $
        for (j = 1; j <= i; j++) {
            printf("%c", prt);
        }
 
        for (k = 1; k <= (i - 1); k++) {
            if (i == 1) {
                continue;
            }
 
            printf("%c", prt);
        }
        printf("\n");
        space--;
    }
 
    space = 1;
 
    for (i = 4; i >= 1; i--) {
        for (sp = space; sp >= 1; sp--) {
            printf(" ");
        }
 
        for (j = 1; j <= i; j++) {
            printf("%c", prt);
        }
 
        for (k = 1; k <= (i - 1); k++) {
            printf("%c", prt);
        }
 
        space++;
        printf("\n");
    }
 
    space = 3;
 
    for (i = 2; i <= 5; i++) {
        if ((i % 2) != 0) {
            for (sp = space; sp >= 1; sp--) {
                printf(" ");
            }
 
            for (j = 1; j <= i; j++) {
                printf("%c", prt);
            }
        }
 
        if ((i % 2) != 0) {
            printf("\n");
            space--;
        }
    }
 
    return 0;
}


Java
// Java Program to print Kite Pattern
import java.io.*;
 
class GFG
{
public static void main(String[] args)
{
 
    int i, j, k, sp, space = 4;
    char prt = '$';
 
    for (i = 1; i <= 5; i++)
    {
 
        // For printing the space
        for (sp = space; sp >= 1; sp--)
        {
            System.out.print(" ");
        }
 
        // For printing the $
        for (j = 1; j <= i; j++)
        {
            System.out.print(prt);
        }
 
        for (k = 1; k <= (i - 1); k++)
        {
            if (i == 1)
            {
                continue;
            }
 
            System.out.print(prt);
        }
        System.out.println();
        space--;
    }
 
    space = 1;
 
    for (i = 4; i >= 1; i--)
    {
        for (sp = space; sp >= 1; sp--)
        {
            System.out.print(" ");
        }
 
        for (j = 1; j <= i; j++)
        {
          System.out.print(prt);
        }
 
        for (k = 1; k <= (i - 1); k++)
        {
            System.out.print(prt);
        }
 
        space++;
        System.out.println();
    }
 
    space = 3;
 
    for (i = 2; i <= 5; i++)
    {
        if ((i % 2) != 0)
        {
            for (sp = space; sp >= 1; sp--)
            {
                System.out.print(" ");
            }
 
            for (j = 1; j <= i; j++)
            {
                System.out.print(prt);
            }
        }
 
        if ((i % 2) != 0)
        {
            System.out.println();
            space--;
        }
    }
}
}
 
// This code is contributed by inder_verma..


Python3
# Python3 Program to print Kite Pattern
if __name__ == '__main__':
 
    space = 4
    prt = '$'
 
    for i in range(1, 6):
 
        # For printing the space
        for sp in range(space, 0, -1):
            print(end = " ")
 
        # For printing the $
        for j in range(1, i + 1):
            print(prt, end = "")
 
        for k in range(1, i):
         
            if (i == 1):
             
                continue
             
            print(prt, end = "")
 
        print()
        space -= 1
     
    space = 1
 
    for i in range(4, 0, -1):
 
        for sp in range(space, 0, -1):
            print(end = " ")
 
        for j in range(1, i + 1):
            print(prt, end = "")
 
        for k in range(1, i):
            print(prt, end = "")
 
        space += 1
        print()
 
    space = 3
 
    for i in range(2, 6):
 
        if ((i % 2) != 0):
         
            for sp in range(space, 0, -1):
                print(" ", end = "")
 
            for j in range(1, i + 1):
                print(prt, end = "")
 
        if ((i % 2) != 0):
            print()
            space -= 1
 
# This code is contributed by
# mohit kumar 29


C#
// C# Program to print Kite Pattern
using System;
 
class GFG
{
public static void Main()
{
 
    int i, j, k, sp, space = 4;
    char prt = '$';
 
    for (i = 1; i <= 5; i++)
    {
 
        // For printing the space
        for (sp = space; sp >= 1; sp--)
        {
            Console.Write(" ");
        }
 
        // For printing the $
        for (j = 1; j <= i; j++)
        {
            Console.Write(prt);
        }
 
        for (k = 1; k <= (i - 1); k++)
        {
            if (i == 1)
            {
                continue;
            }
 
            Console.Write(prt);
        }
        Console.WriteLine();
        space--;
    }
 
    space = 1;
 
    for (i = 4; i >= 1; i--)
    {
        for (sp = space; sp >= 1; sp--)
        {
            Console.Write(" ");
        }
 
        for (j = 1; j <= i; j++)
        {
            Console.Write(prt);
        }
 
        for (k = 1; k <= (i - 1); k++)
        {
            Console.Write(prt);
        }
 
        space++;
        Console.WriteLine();
    }
 
    space = 3;
 
    for (i = 2; i <= 5; i++)
    {
        if ((i % 2) != 0)
        {
            for (sp = space; sp >= 1; sp--)
            {
                Console.Write(" ");
            }
 
            for (j = 1; j <= i; j++)
            {
                Console.Write(prt);
            }
        }
 
        if ((i % 2) != 0)
        {
            Console.WriteLine();
            space--;
        }
    }
}
}
 
// This code is contributed by Soumik


PHP
= 1; $sp--)
    {
        echo (" ");
    }
 
    // For printing the $
    for ($j = 1; $j <= $i; $j++)
    {
        echo ( $prt);
    }
 
    for ($k = 1; $k <= ($i - 1); $k++)
    {
        if ($i == 1)
        {
            continue;
        }
 
        echo ( $prt);
    }
echo ("\n");
$space--;
}
 
$space = 1;
 
for ($i = 4; $i >= 1; $i--)
{
    for ($sp = $space; $sp >= 1; $sp--)
    {
        echo (" ");
    }
 
    for ($j = 1; $j <= $i; $j++)
    {
        echo ( $prt);
    }
 
    for ($k = 1; $k <= ($i - 1); $k++)
    {
        echo ( $prt);
    }
 
    $space++;
    echo ("\n");
}
 
$space = 3;
 
for ($i = 2; $i <= 5; $i++)
{
    if (($i % 2) != 0)
    {
        for ($sp = $space; $sp >= 1; $sp--)
        {
            echo (" ");
        }
 
        for ($j = 1; $j <= $i; $j++)
        {
            echo ( $prt);
        }
    }
 
    if (($i % 2) != 0)
    {
        echo ("\n");
        $space--;
    }
}
 
// This code is contributed
// by Sach_Code
?>


Javascript


输出:
$
   $$$
  $$$$$
 $$$$$$$
$$$$$$$$$
 $$$$$$$
  $$$$$
   $$$
    $
   $$$
  $$$$$

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。