c语言中提供了for语句、while语句和do-while三种循环语句
1.for语句的基本语法:
for语句的一般形式为:for(表达式1;表达式2;表达式3)循环体语句;
for语句的执行过程如下:第1步:首先计算表达式1。第2步:判断表达式2,若其值为真(非0),则执行循环体语句,然后执行第3步;若值为假(0),结束循环,转到第五步执行。第3步:计算表达式3。第4步:返回第2步继续执行。第5步:循环结束,继续执行for语句的下一条复合语句。
2.while语句的基本语法:
while的一般形式:while(表达式)循环体;
while语句的执行过程:第1步:计算表达式的值,若表达式的值为“真”,则执行第2步;若表达式的值为“假”,则转到第4步。第2步:执行循环体语句。第3步:返回第1步。第4步:结束循环,执行while语句的下一条语句。
3.do-wile语句的基本语法:
do-wile语句的一般形式:do循环体? while(表达式);
第1步:执行循环体语句。第2步:计算表达式的值,若表达式的值为“真”(非0),返回第1步;若表达式的值为“假”(0),则执行第3步。第3步:结束循环,执行do-while语句的下一条语句
4.break语句:
当break用于循环体语句中时,可使程序终止循环而转去执行循环语句的后续语句。
5.continue语句
continue语句的作用是跳过循环体中continue后面的语句,继续下一次循环。
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;
}
?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 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;
}
?5.5
#include<stdio.h>
int main(void)
{
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(void)
{
int i;
float x,max;
printf("请输入第1个数:");
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;
}
?5.7
#include<stdio.h>
int main(void)
{
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(void)
{
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(void)
{
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(void)
{
int i;
char ch;
for (i = 1; (ch = getchar()) != '\n'; i++)
putchar(ch - 32);
return 0;
}
?5.11
#include<stdio.h>
int main(void)
{
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;
}
?5.12
#include<stdio.h>
int main(void)
{
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.13
#include<stdio.h>
int main(void)
{
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 = i + 1;
}
printf("%ld!=%1d\n", n, fact);
return 0;
}
5.15
?
#include<stdio.h>
int main(void)
{
int digit, letter, other = 0;
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;
}
?5.16
#include<stdio.h>
int main(void)
{
int i, sum;
i = 1; sum = 0;
do
{
sum = sum + i;
i = i + 1;
} while (i <= 100);
printf("sum=%d\n", sum);
return 0;
}
?5.17
#include<stdio.h>
int main(void)
{
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(void)
{
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(void)
{
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(void)
{
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 = i + 1;
goto loop;
}
printf("sum=%d\n", sum);
return 0;
}
?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");
}
return 0;
}
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 = 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>
#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;
}
}
}
?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;
}
?
|