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

循环结构的程序设计

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

“直到型”循环语句有do_while

while语句

五个数字的乘积:

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

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

#include
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 
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
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");
 }
}

你可能感兴趣的:(程序语言)