#include<stdio.h>
#include<math.h>
int main()
{
int n = 13, year;
double number, rate = 0.02;
for(year=1;year<=10;year++){
number = n * pow((1 + rate), year);
printf("%2d年后,人数为:%.2f亿\n", year, number);
}
return 0;
}#include<stdio.h>
#include<math.h>int main() { double sum, item, flag, denominator; sum = 0; item = 1; denominator = 1; flag = 1; while (fabs(item) >= 1e-6) { sum = sum + item; flag = -flag; denominator = denominator + 3; item = flag / denominator; }printf(“sum=%f\n”, sum); return 0;}
for语句的一般形式为:
for(表达式1;表达式2;表达式3)
循环体语句;
for语句的执行过程如下:
1首先计算表达式1。
2判断表达式2,若其值为真(非0),则执行循环体语句,然后行第③步;若值为假(0),结束循环,转到第⑤步执行。
③计算表达式3。
④返回第②步继续执行。
5循环结束,继续执行for语句的下一条语句。
大部分情况下,循环体语句为一复合语句。
注意,表达式1只是在进入循环之前计算一次。表达式2、循环体语句和表达式3将重复执行。根据for语句格式的特点,其实际应用可以有很多种形式。
循环体语句;
说明:循环变量赋初值是一个赋值语句,用于给循环变量赋初值;循环条件是一个关系表达式,决定何时终止循环(即确定循环的终值);循环变量增量决定循环变量在完成一次循环后如何变化。三部分之间以“;”隔开。
#include<stdio.h>
int main() {
int i, n, sum;
scanf_s("%d", &n);
sum = 0;
for (i = 1; i <= n; i++)
sum = sum + i;
printf(“由1到%d的和是:%d\n”, n,sum);
return 0;
}
for语句的一般形式中省略表达式1格式如下:
for(;表达式2;表达式3)
循环体语句;
说明:省略表达式1时,可以将循环变量赋初值放在for之前。注意,此时不能省略第一个“;”。
for语句的一般形式中省略表达式2格式如下:
for(表达式1;;表达式3)
循环体语句;
说明:省略表达式2时,表示不对循环进行控制,这时如果没有其他处理的话,会形成死循环。应避免这样使用for结构。
for语句的一般形式中省略表达式3格式如下:
for(表达式1;表达式2;)循环体语句;
说明:省略表达式3时,可以在循环语句中加入修改循环变量的值的语句。
#include<stdio.h>
int main() { int i, n; double factorial; printf(“输入n的值:”); scanf_s("%d", &n); factorial = 1; for (i = 1; i <= n; i++) factorial = factorial * i; printf("%d!=%.0f\n", n, factorial); return 0;}#include<stdio.h>
int main() {
int n, i, denominator, flag;
float sum, item;
printf(“输入n的值:”);
scanf_s("%d", &n);
flag = 1;
denominator = 1;
sum = 0;
for (i = 1; i <= n; i++) {
item = flag * 1.0 / denominator;
sum = sum + item;
flag = -flag;
denominator = denominator + 3;
}printf(“sum=%.2f\n”, sum);
return 0;
}
#include<stdio.h>
int main() {
int i;
float x, max;
printf(“输入第一个数:”);
scanf_s("%f", &x);
max = x;
for (i = 1; i <= 9; i++) {
printf(“输入第%d个数:”, i + 1);
scanf_s("%f", &x);
if (x > max)
max = x;
}printf(“10个数的最大值是:%.0f”, max);
return 0;
}#include<stdio.h>
int main() { int number, a, b, c; for (number = 100; number <= 999; number++) { a = number / 100; b = number % 100 / 10; c =number % 10; if (number == a * a * a + b * b * b + c * c * c) printf("%5d", number); } return 0;}
#include<stdio.h>
int main() { int number, sum, i; printf(“输入一个正整数:”); scanf_s("%d", &number); sum = 0; for (i = 1; i <= number - 1; i++) if (number % i == 0) sum = sum + 1; if (number == sum) printf("%d是完整数\n", number); else printf("%d不是完整数\n", number); return 0;}
#include<stdio.h>
int main() { int upper, lower, digit, i, other; char ch; upper = lower = digit = other = 0; printf(“输入10个字符:”); for (i = 1; i <= 10; i++) { ch =getchar(); if (ch >= ‘a’ && ch <= ‘z’) lower++; else if (ch >= ‘A’ && ch < ‘Z’) upper++; else if (ch >= ‘0’ && ch <= ‘9’) digit++; else other++; }printf(“小写字母%d个,大写字母%d个,数字%d个,其他字符%d个\n”, lower, upper, digit, other); return 0;}
#include<stdio.h>
int main() { int i; char ch; for (i = 1; (ch = getchar()) != ‘\n’; i++) putchar(ch - 32); return 0;} #include<stdio.h>
int main() { int n = 0, i; char ch; printf(“输入3个数字:”); for (i = 1; i <= 3; i++) { scanf_s("%c",& ch); n = n * 10 + ch - ‘0’; } printf("%d\n", n); return 0;}
#include<stdio.h>
int main() { int i, flag, number; printf(“输入一个正整数:”); scanf_s("%d", &number); flag = 1; for (i = 2; i <= number - 1 && flag; i++) if (number % i == 0) flag = 0; if (flag) printf("%d是素数\n", number); else printf("%d不是素数\n", number); return 0;}
while语句的基本语法:while语句在使用时,总要先判断一个条件,所以可以用while语句实现当型循环。while语句的一般形式如下:while(表达式)循环体;
#include<stdio.h>
int main() { int i, sum; i = 1; sum = 0; while (i <= 100) { sum = sum + i; i = i + 1; } printf(“sum=%d\n”, sum); return 0;}
#include<stdio.h>
int main() { int i; long n, fact; i = 2; fact = 1; printf(“输入n的值”); scanf_s("%ld", &n); while (i <= n) { fact = fact * i; i = i + 1; }printf("%ld!=%ld\n", n, fact); return 0;}
#include<stdio.h>
int main() { int digit, letter, other; char ch; digit = letter = other = 0; printf(“输入一串字符:”); while ((ch = getchar()) != ‘\n’) if ((ch >= ‘0’) && (ch <= ‘9’)) digit++; else if ((ch >= ‘a’&&ch<=‘z’) || (ch >= ‘A’ && ch <= ‘Z’)) letter++; else other++; printf(“数字%d个,字母%d个,其他%d个\n”, digit, letter, other); return 0;}
do-while语句实现循环结构
do-while语句在使用时,首先执行循环体语句,然后再判断条件。所以可以用do-while语句实现“直到型”循环。do-while语句的一般形式如下:do
循环体
while(表达式);
#include<stdio.h>
int main() { int i, sum; i = 1; sum = 0; do { sum = sum + i; i = i + 1; } while (i <= 100); printf(“sum=%d\n”, sum); return 0;}
#include<stdio.h>
int main() { int a, b, r, n, m; printf(“输入两个整数:”); scanf_s("%d%d", &a, &b); m = a, n = b; do { r = a % b; a = b; b = r; } while (r != 0); printf("%d和%d的最大公约数是:%d\n", m, n, a); printf(“最小公倍数是:%d”, m * n / a); return 0;}
#include<stdio.h>
int main() { long n, m; int count = 0; printf(“输入一个整数:”); scanf_s("%ld", &n); m = n; if (n < 0)n = -n; do { n = n / 10; count++; } while (n != 0); printf(“整数%d有%d位数\n”, m, count); return 0;}
#include<stdio.h>
int main() { int i = 5; do { if (i % 3 == 1) if (i % 5 == 2) { printf("%d", i); break; }i++; } while (i != 0); return 0;}
#include<stdio.h>
#include<math.h>int main() { int n, m, i; printf(“输入一个正整数:”); scanf_s("%d", &n); m = sqrt(n); for (i = 2; i <= m; i++) if (n % i == 0) break; if (i > m) printf(" % d是素数:\n", n); else printf(" %d不是素数:\n", n); return 0;}
#include<stdio.h>
int main() { int num, n; float score, total = 0; num = 0; n = 0; while (1) { printf(“输入分数#%d(0-100):”,n + 1); scanf_s("%f", &score); if (score < 0) break; if (score < 60) num++; total = total + score; n++; } printf(“平均分数是:%.2f\n”, total / n); printf(“不及格的有:%d.\n”, num); return 0;}
#include<stdio.h>
int main() { int i, n = 1; for (i = 1; i <= 100; i++) { if (i % 7 != 0) continue; printf("%4d", i); if (n++ % 5 == 0)printf("\n"); } return 0;}#include<stdio.h>
int main() { int n, s = 0; n = 1; while (n < 10) { s = s + n; if (s > 5) break; if (n % 2 == 1) continue; n++; }printf(“s=%d,n=%d\n”, s, n); return 0;}
#include<stdio.h>
int main() {
int i, sum;
i = 1; sum = 0;
loop:if (i <= 100) {
sum = sum + i;
i = i + 1;
goto loop;
}
printf(“sum=%d\n”, sum);
return 0;
}
5.6循环嵌套
当一个循环的循环体内又包含了另外一个完整的循环结构时,称为循环的嵌套或者是三重循环。
使用for语句,while语句及do-while语句相互嵌套,构成的嵌套结构有以下几种。
for语句中嵌套for语句
for(){
…
for()
{…}
}
for语句中嵌套while语句
for()
{ …
while()
{…}
}
while语句中嵌套while语句
while()
{ …
while()
{…}
…
}
do-while语句中嵌套for语句
do
{ …
for()
{…}
…
}while();
do-while语句中嵌套do-while语句
do
{ …
do
{ …
}while();
…
}while();
while语句中嵌套do-while语句
while()
{ …
do
{…
}while();
…
}#include<stdio.h>
int main() { int i, j; double f, s = 0; for (i = 1; i <= 10; i++) { f = 1; for (j = 1; j <= i; j++) f = f * j; s = s + f; } printf(“1!+2!+3!+…+10!=%.0f\n”, s); return 0;}
#include<stdio.h>
int main() { int i, j; for (i = 1; i <= 9; i++) { for (j = 1; j <= i; j++) printf("%d*%d=%d\t", j,i,j * i); printf("\n"); } return 0;}
#include<stdio.h>
#include<math.h>int main() { int i, n, k, count = 0; n = 2; while (n < 100) { k = sqrt(n); for (i = 2; i <= k; i++) if (n % i == 0)break; if (i > k) { printf("%4d", n); if (++count % 10 == 0)printf("\n"); } n++; } return 0;}
#include<stdio.h>
int main() { int i, n, m; for(m=10;m<=20;m++) { n = m, i = 2; printf("%d=", n); do { if (n % i == 0) { printf("%d*", i); n = n / i; } else { i++; } } while (n != i); printf("%d\n", n); } return 0;}
1.定义整数变量days,d1,d2
2.days=9
3.d2=1
4.d1=(d2+1)2
5.d2=d1
6.–days
7.days>0值为真返回4否则输出d1#include<stdio.h>
int main(){ int days, d1, d2; days = 9; d2 = 1; do { d1 = (d2 + 1) * 2; d2 = d1; --days; } while (days > 0); printf(“第一天摘了%d\n”, d1); return 0;}
.定义整数变量n=1,小数变量x,fz,fm=1,sinx
2.给x赋值3.fz=sinx=x4.n=n+15.fz = -fz * x * x6.fm = fm * (2 * n - 2) * (2 * n - 1)7.sinx = sinx + fz / fm8.fabs(fz/ fm) > eps值为真返回4,否则转99.输出x,sinx#include<stdio.h>
#include<math.h>#define eps 1e-6int main(){ int n = 1; float x; double fz, fm = 1, sinx; printf(“输入x的值:”); scanf_s("%f", &x); fz = x; sinx = x; do { n = n + 1; fz = -fz * x * x; fm = fm * (2 * n - 2) * (2 * n - 1); sinx = sinx + fz / fm; } while (fabs(fz/ fm) > eps); printf(“sin(%f)=%.6f\n”, x, sinx); printf(“sin(%f)=%.6f\n”, x, sin(x)); return 0;}
.定义小数变量x1,x0,f,f1
2.x1=1.03.x0=x14.f = ((2 * x0 - 4) * x0 + 3) * x0 - 65.f1 = (6 * x0 - 8) * x0 + 36.x1 = x0 - f / f17.fabs(x1 - x0) > eps值为真返回3,否则输出x1#include<stdio.h>
#include<math.h>#define eps 1e-6int main(){ float x1, x0, f, f1; x1 = 1.0; do { x0 = x1; f = ((2 * x0 - 4) * x0 + 3) * x0 - 6; f1 = (6 * x0 - 8) * x0 + 3; x1 = x0 - f / f1; } while (fabs(x1 - x0) > eps); printf("%6.2f", x1); return 0;}1
.定义整数变量m,w,c
2.m=03.m<=9值为真转4,否则结束4.w=05.w<=12值为真走6,否则转106.c=36-m-w7.m * 4 + w * 3 + c * 0.5 == 36值为真走8,否则转98,输出m,w,c9.w++返回510.m++,返回3#include<stdio.h>
int main(){ int m, w, c; for (m = 0; m <= 9; m++) { for (w = 0; w <= 12; w++) { c = 36 - m - w; if (m * 4 + w * 3 + c * 0.5 == 36) { printf(“男:%d,女:%d,小孩:%d\n”, m, w, c); } } } return 0;}1.定义整数变量i,j,k,n=0
2.i=13.i<5值为真走4,否则输出n4.j=15.j<5值为真走7否则转66.i++返回37.k=18.k<5值为真走10,否则转99j++返回510.(i != k && i != j && j != k值为真走12否则转1111.k++返回812.输出j,k13.++n % 5 == 0值为真输出换行返回11,否则返回11#include<stdio.h>
int main(){ int i, j, k, n = 0; for (i = 1; i < 5; i++) for (j = 1; j < 5; j++) for (k = 1; k < 5; k++) if (i != k && i != j && j != k) { printf("%d%d%d\t", i, j, k); if (++n % 5 == 0) printf("\n"); } printf("\n有:%d\n", n); return 0;}
1.定义整数变量i,j
2.i=13.i<=5值为真走4,否则结束4.j=15.j<=20-i值为真走6,否则转86.输出空格7.j++返回58.j=19.j<=2i-1值为真走10,否则转1210.输出11.j++返回912.输出换行13.i++返回3