C 语言箭头星形图案程序
给定一个数 n,我们必须打印最多 n 个星号的箭头星形图案。
输入 4 的星形图案如下所示 −

示例
Input: 3 Output:

Input: 5 Output:

下面使用的方法如下 −
- 以整型输入。
- 然后打印 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)
示例
#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("
");
}
// 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("
");
}
return 0;
}
int main() {
// get the value from user
int num = 4;
// function calling
arrow(num);
return 0;
}输出
如果运行上述代码,它将生成以下输出 −

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP