首页 > 后端开发 > C++ > 正文

C程序的箭头星型图案

PHPz
发布: 2023-08-25 17:09:05
转载
1280 人浏览过

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
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!