例5.1
代码
#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;
}
运行结果
例5.2
代码
#include<stdio.h>
#include<math.h>
int main() {
double sum, item, flag, denominator;
sum = 0;
item = 1;
flag = 1;
denominator = 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的一般形式为
for(表达式1;表达式2;表达式3)
循环体语句
例5.3
#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;
}
运行结果
例5.4
代码
#include<stdio.h>
int main(void) {
int i, n;
double factoral;
printf("输入n的值:");
scanf_s("%d",&n);
factoral = 1;
for (i = 1;i <= n;i++) {
factoral = factoral * i;
}
printf("%d!=%.0f\n",n,factoral);
return 0;
}
运行结果 例5.5
代码
#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;
}
运行结果
例5.6
代码
#include<stdio.h>
int main() {
int i;
float x, max;
printf("请输入第1个数:s");
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);
}
运行结果
例5.7
#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;
}
运行结果
例5.8
代码
#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 + i;
}
}
if (number==sum) {
printf("%d是完数\n",number);
}
else {
printf("%d不是完数\n",number);
}
return 0;
}
运行结果
例5.9
代码
#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;
}
运行结果
例5.10
代码
#include<stdio.h>
int main() {
char ch;
for (;(ch = getchar()) != '\n';) {
putchar(ch-32);
}
return 0;
}
运行结果
例5.11
代码
#include<stdio.h>
int main() {
int n = 0,i;
char ch;
printf("请输入3个数字:");
for (i = 1;i <= 3;i++) {
scanf_s("%c",&ch,3);
n = n * 10 + ch - '0';
}
printf("%d\n",n);
return 0;
}
运行结果
例5.12
代码
#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;
}
运行结果
5.3使用while语句实现循环结构
例5.13
代码
#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;
}
运行结果
例5.14
代码
#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 += 1;
}
printf("%ld!=%ld\n",n,fact);
return 0;
}
运行结果
例5.15
代码
#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语句实现循环结构
例5.16
代码
#include<stdio.h>
int main() {
int i, sum;
i = 1;
sum = 0;
do {
sum = sum + i;
i += 1;
} while (i<=100);
printf("sum=%d\n",sum);
return 0;
}
运行结果
例5.17
代码
#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;
}
运行结果
例5.18
代码
#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("整数%ld有%d位数\n",m,count);
return 0;
}
运行结果
例5.19
代码
#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;
}
运行结果
例5.20
代码
#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;
}
运行结果
例5.21
代码
#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;
}
运行结果
例5.22
代码
#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;
}
运行结果
例5.23
代码
#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;
}
运行结果
例5.24
代码
#include<stdio.h>
int main() {
int i, sum;
i = 1;
sum = 0;
loop:if (i<=100) {
sum = sum + i;
i += 1;
goto loop;
}
printf("sum=%d\n",sum);
return 0;
}
运行结果
5.6循环嵌套
例5.25
代码
#include<stdio.h>
int main() {
int i, j;
double factorial,s=0;
for (i = 1;i <= 10;i++) {
factorial = 1;
for (j = 1;j <= i;j++) {
factorial = factorial * j;
}
s = s + factorial;
}
printf("1!+2!+3!+···+10!=%.0f\n",s);
return 0;
}
运行结果
例5.26
代码
#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");
}
}
运行结果
例5.27
代码
#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;
}
运行结果
例5.28
代码
#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;
}
运行结果
典型算法举例
例5.29
代码
#include<stdio.h>
int main() {
int day, d1, d2;
day = 9;
d2 = 1;
do {
d1 = (d2 + 1) * 2;
d2 = d1;
--day;
} while (day>0);
printf("第一天摘了%d\n",d1);
return 0;
}
运行结果
例5.30
代码
#include<stdio.h>
#include<math.h>
#define eps 1e-6
int main() {
int n = 1;float x;
double fz, fm = 1, sinx;
printf("输入x的值:");
scanf_s("%f",&x);
fz = x;
sinx = x;
do {
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)=%0.6f\n",x,sinx);
printf("sin(%f)=%0.6f\n",x,sin(x));
return 0;
}
运行结果
例5.31
代码
#include<stdio.h>
#include<math.h>
#define eps 1e-6
int 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;
}
运行结果
例5.32
代码
#include<stdio.h> int main() { int men, women, child; for (men = 0;men <= 9;men++) { for (women = 0;women <= 12;women++) { child = 36 - men - women; if (men*4+women*3+child*0.5==36) { printf("男%d女%d小孩%d\n",men,women,child); } } } return 0; } 运行结果
例5.33
代码
#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;
}
运行结果
循环程序设计示例
例5.34
代码
#include<stdio.h>
int main() {
int i, j;
for (i = 1;i <= 5;i++) {
for (j = 1;j <= 20-i;j++) {
printf(" ");
}
for (j = 1;j <= 2 * i - 1;j++) {
printf("*");
}
printf("\n");
}
return 0;
}
运行结果
例5.35
代码
#include<stdio.h>
int main() {
int i, j;
for (i = 1;i <= 5;i++) {
for (j = 1;j <= 20-i;j++) {
printf(" ");
}
for (j = 1;j <= 2 * i - 1;j++) {
printf("*");
}
printf("\n");
}
return 0;
}
运行结果
例5.35
代码
#include<stdio.h>
#include<stdlib.h>
int main() {
int m, n, count = 0;
m = rand() % (80 - 10 + 1) + 10;
printf("请输入一个10-80之间的整数:");
while (1) {
scanf_s("%d",&n);
count++;
if (m==n) {
printf("恭喜!你猜对了,你真棒!\n");
break;
}else if (m > n && count < 5) {
printf("对不起,你猜小了,再来一次!");
}
else if (m < n && count < 5) {
printf("对不起,你猜大了,再来一次!");
}
if (count==5) {
printf("对不起,你没有机会了!\n这个数是:%d游戏结束!\n",m);
break;
}
}
return 0;
}
运行结果
例5.36
代码
#include<stdio.h>
#include<math.h>
int main() {
int x,i, j = 0, n, k = 0;
for (x = 100;x < 1000;x++) {
k = sqrt(x);
for (i = 2;i <= k;i++) {
if (x % i == 0) {
break;
}
if (i > k) {
k = x;
n = 0;
while (k > 0) {
n = n * 10 + k % 10;
k /= 10;
}
if (x == n) {
printf("%d\t", x);
if (++j % 5 == 0) {
printf("\n");
}
}
}
}
}
return 0;
}
运行结果
例5.37代码
#include<stdio.h>
int main() {
int x, t;
printf("i\tpower\n");
for (x = 100;x < 1000;x++) {
t = x * x;
while (t != 0) {
if (x == t % 1000) {
printf("%d\t%d\n", x, x * x);
break;
}
else {
t = t / 10;
}
}
}
return 0;
}
运行结果