C ++程序打印钻石形状
这是一个用于打印菱形的C++程序。
算法
Begin
Take the no of rows n means the dimension of the diamond shape as input.
Declare the variables i, j and initialize space=1.
Initialize space = n-1.
Run for loop till n.
Run for loop to print space.
Decrease space.
Run for loop to print stars.
Now do the same thing 以相反的顺序。
Initialize space = 1.
Run for loop till n.
Run for loop to print space.
Increase space.
Run for loop to print stars.
End示例
#include<iostream>
using namespace std;
main() {
int n, i, j, space=1;
cout<<"Enter number of rows : ";
cin>>n;
space=n-1;
for (i=1; i<=n; i++) {
for(j=1; j<=space; j++) {
cout<<" "; //print space.
}
space--;
for(j=1; j<=(2*i-1); j++) {
cout<<"*"; //print stars.
}
cout<<"\n";
}
//以相反的顺序。
space=1;
for (i=1; i<=n; i++) {
for(j=1; j<=space; j++) {
cout<<" "; //print space.
}
space++;
for(j=1; j<=(2*(n-i)-1); j++) {
cout<<"*"; //print stars.
}
cout<<"\n";
}
}输出结果
Enter number of rows: 4 * *** ***** ******* ***** *** *
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短