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 −

Input: 3 Output:

Input: 5 Output:

下面使用的方法如下:
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)演示
#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程序的箭头星型图案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号