您的位置:首页 > 编程语言 > C语言/C++

C语言--程序设计基础--6章

2015-07-15 23:09 218 查看

循环结构的程序设计

“当型”循环语句有while语句和for语句

“直到型”循环语句有do_while

while语句

五个数字的乘积:

#include <stdio.h>
int main(void) {
int i=1;
int s=1;
while (i <= 5) {
s*=i;
i++;
}
printf("%d", s);
}


打印九九乘法表(while语句):

#include<stdio.h>
int main(void) {
int j = 0, i = 0;
while(j < 9) {
j++;
i = 1;
while(i <= j) {
printf("%d*%d=%d\t", i, j, i * j);
i++;
}
printf("\n");
}
}


打印九九乘法表(for语句):

#include <stdio.h>
int main(void) {
int i, j, s;
for (i = 1; i <= 9; i++) {
for (j = 1; j <= i; j++) {
s = i * j;
printf("%d*%d=%d\t", j, i, s);
}
printf("\n");
}
}


用for语句打印出以“*”组成的心形图案:

#include<stdio.h>
int main(void) {
int x, y;
for(y = 0; y <= 8; y++) {
for(x = -6; x < 0; x++) {
if(y <= (8 + x) && x >= -6 && y >= (-x - 5) && y >= 0 && y >= (3 + x) && x <= 0) {
printf("*");
} else {
printf(" ");
}
}
for(x = 0; x <= 6; x++) {
if(y <= (8 - x) && x <= 6 && y >= (x - 5) && y >= 0 && y >= (3 - x) && x >= 0) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: