首頁  >  文章  >  後端開發  >  C程式的箭頭星型圖案

C程式的箭頭星型圖案

PHPz
PHPz轉載
2023-08-25 17:09:051300瀏覽

Given a number n and we have to print the arrow star pattern of maximum n number of stars.

The star pattern of input 4 will look like −

C程式的箭頭星型圖案

Example

Input: 3
Output:
C程式的箭頭星型圖案

Input: 5
Output:
C程式的箭頭星型圖案

#下面使用的方法如下:
  • 以整數形式輸入。
  • 然後印出n個空格和n個星號。
  • 遞減直到n>1。
  • 現在遞增直到n。
  • 並依照遞增順序列印空格和星號。

演算法

Start
In function int arrow(int num)
   Step 1-> declare and initialize i, j
   Step 2-> Loop For i = 1 and i <= num and i++
      Loop For j = i and j < num and j++
         Print a space
      Loop For j = i and j <= num and j++
         Print "*"
         Print newline
   Step 3-> Loop For i = 2 and i <= num and i++
      Loop For j= 1 and j < I and j++
         Print a space
      Loop For j = 1 and j <= i and j++
         Print "*"
         Print newline
In function int main()
   Step 1-> declare and initialize num = 4
   Step 2-> call arrow(num)

Example

 示範

#include <stdio.h>
// arrow function
int arrow(int num) {
   int i, j;
   // Prints the upper part of the arrow
   for (i = 1; i <= num; i++) {
      // to print the spaces
      for (j = i; j < num; j++) {
         printf(" ");
      }
      // to print the * for the pattern
      for (j = i; j <= num; j++) {
         printf("*");
      }
      printf("</p><p>");
   }
   // Prints lower part of the arrow
   for (i = 2; i <= num; i++) {
      // to print the spaces
      for (j = 1; j < i; j++) {
         printf(" ");
      }
      // to print the * for the pattern
      for (j = 1; j <= i; j++) {
         printf("*");
      }
      printf("</p><p>");
   }
   return 0;
}
int main() {
   // get the value from user
   int num = 4;
   // function calling
   arrow(num);
   return 0;
}

輸出

如果執行上述程式碼,將產生下列輸出−

C程式的箭頭星型圖案

###

以上是C程式的箭頭星型圖案的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除