while的框架如下图所示。
总结一句话就是先判断,再执行。
例题5-1
#include
//1+2+3+...100
//本题要求使用while的方法。
int i = 1;
int sum = 0;
int main(void)
{
while (i <= 100)
{
sum = sum + i;
i = i + 1;
}
printf("1+2+3+4+5...+100 = %d", sum);
return 0;
}
#include
int i = 1;
int sum = 0;
int main(void)
{
do
{
sum = sum + i;
i = i + 1;
} while(i<=100);
printf("1+2+3+...100 = %d", sum);
return 0;
}
#include
int main()
{
int i, sum = 0;
printf("please enter i,i=?");
scanf("%d", &i);
while (i <= 10)
{
sum = sum + i;
i++;
}
printf("sum=%d\n", sum);
return 0;
}
#include
int main()
{
int i, sum = 0;
printf("please enter i,i=?");
scanf("%d", &i);
do
{
sum = sum + i;
i++;
} while (i <= 10);
printf("sum=%d\n", sum);
return 0;
}
#include
#define total_amount 10000
int sum = 0;
int amount = 0;
int i = 0;
double average = 0.0;
int main(void)
{
for (i = 1; i <= 1000; i++)
{
printf("please enter amount:");
scanf("%d", &amount);
sum = sum + amount;
if (sum >= 10000)
{
break;
}
}
printf("sum = %d, i = %d, average = %f", sum, i, (double)sum/ (double)i);
return 0;
}
#include
int i = 0;
int main(void)
{
for (i = 100; i <= 200; i++)
{
if (i % 3 == 0)
continue;
printf("%d ", i);
}
return 0;
}
#include
int i = 0, j = 0;
int main(void)
{
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 5; j++)
printf("%d\t", i * j);
printf("\n");
}
return 0;
}
#include
double sum = 0.0;
int i = 1;
int sign = 1;
double pi = 0.0;
int main(void)
{
while (1.0 / i >= 1e-6)
{
sum = sum + sign * (1.0 / i);
sign = -sign;
i = i + 2;
}
pi = sum * 4;
printf("pi=%10.8f", pi);
return 0;
}
#include
int n = 0;
int sum = 0;
int f1 = 1,f2 = 1,f3 = 0;
//1 1 2 3 5 8
int main(void)
{
printf("%9d\n%9d\n", f1, f2);
for (n = 1; n <= 38; n++)
{
sum = f1 + f2;
printf("%9d\n", sum);
f1 = f2;
f2 = sum;
}
return 0;
}
#include
int n = 0;
int sum = 0;
int f1 = 1, f2 = 1, f3 = 0;
//1 1 2 3 5 8
int main(void)
{
for (n = 1; n <= 20; n++)
{
printf("%12d%12d", f1,f2);
f1 = f1 + f2;
f2 = f1 + f2;
if (n % 2 == 0)
printf("\n");
}
return 0;
}
#include
#include
int n = 0, i = 0;
int main(void)
{
printf("please type a number\n");
scanf("%d", &n);
for (i = 2; i <= n - 1; i++)
{
if (n % i == 0)
{
printf("this number is not prime");
break;
}
}
if(i == n)
printf("this number is prime");
return 0;
}
#include
#include
int n = 0, i = 0;
int main(void)
{
printf("please type a number\n");
scanf("%d", &n);
for (i = 2; i <= (int)sqrt(n); i++)
{
if (n % i == 0)
{
printf("this number is not prime");
break;
}
}
if (i == (int)sqrt(n) + 1)
printf("this number is prime");
return 0;
}
#include
#include
int n = 0, i = 0;
int number = 0;
int main(void)
{
for (number = 100; number <= 200; number++)
{
for (i = 2; i <= (int)sqrt(number); i++)
{
if (number % i == 0)
{
//printf("this number is not prime\n");
break;
}
}
if (i == (int)sqrt(number) + 1)
printf("%d is prime\n",number);
}
return 0;
}
#include
char passward = 0;
int main(void)
{
printf("please type passward\n");
while ((passward = getchar())!= '\n')
{
if (('a' <= passward && passward <= 'z') || ('A' <= passward && passward <= 'Z'))
{
if(('a' <= passward && passward <= 'v')|| ('A' <= passward && passward <= 'V'))
passward = passward + 4;
else
passward = passward - 22;
}
else
{ }
printf("%c", passward);
}
return 0;
}
#include
double sum = 0.0;
int i = 1;
int sign = 1;
double pi = 0.0;
int count = 0;
int main(void)
{
while (1.0 / i >= 1e-6)
{
sum = sum + sign * (1.0 / i);
sign = -sign;
i = i + 2;
count = count + 1;
}
pi = sum * 4;
printf("pi=%10.8f\n", pi);
printf("count = %d\n", count);
return 0;
}
#include
double sum = 0.0;
int i = 1;
int sign = 1;
double pi = 0.0;
int count = 0;
int main(void)
{
while (1.0 / i >= 1e-8)
{
sum = sum + sign * (1.0 / i);
sign = -sign;
i = i + 2;
count = count + 1;
}
pi = sum * 4;
printf("pi=%10.8f\n", pi);
printf("count = %d\n", count);
return 0;
}
#include
unsigned int m = 0, n = 0;
unsigned int max = 0, min = 0;
unsigned int i = 0;
unsigned int a1 = 0 , a2 = 0;
int main(void)
{
printf("please type m value and n value\n");
scanf("%d%d", &m, &n);
//compare m value and n value
if (m > n)
{
max = m;
min = n;
}
else
{
max = n;
min = m;
}
for (i = 2; i <= min; i++)
{
if (m % i == 0 && n % i == 0)
{
a1 = i;
}
}
for (i = max; i <= min*max; i++)
{
if (i % m == 0&& i % n == 0)
{
a2 = i;
break;
}
}
printf("a1=%d\n", a1);
printf("a2=%d\n", a2);
return 0;
}
#include
int p = 0, r = 0, m = 0, n = 0, temp = 0;
int main(void)
{
printf("please type m value and n value:");
scanf("%d%d", &n, &m);
if (n < m);//make sure n value > m value
{
temp = n;
n = m;
m = temp;
}
p = n * m;
while (m != 0)
{
r= n % m ;
n = m;
m = r;
}
printf("最大公约数为:%d\n",n);
printf("最小公倍数为:%d\n",p/n);
return 0;
}
习题5-4
#include
//count1 英语字符个数
//count2 空格字符个数
//count3 数字字符个数
//count4 其他字符个数
char a = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int main(void)
{
printf("please type your message:");
while ((a= getchar()) != '\n')
{
if (('A' <= a && a <= 'Z') || ('a' <= a && a <= 'z'))
count1 = count1 + 1;
else if (a == ' ')
count2 = count2 + 1;
else if (('0' <= a && a <= '9'))
count3 = count3 + 1;
else
count4 = count4 + 1;
}
printf("count1 = %d,count2 = %d,count3 = %d,count4 = %d\n", count1, count2, count3, count4);
return 0;
}
#include
#include
int n = 0;
int sum1 = 0;
int sum = 0;
int a = 0;
int main(void)
{
printf("please type a value and n value:");
scanf("%d%d",&a,&n);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
sum1 = sum1 + a * (int)pow(10,j-1);
}
sum = sum + sum1;
sum1 = 0;
}
printf("sum = %d", sum);
return 0;
}
#include
double sum1 = 1;
double sum = 0;
int a = 0;
int main(void)
{
for (int i = 1; i <= 20; i++)
{
for (int j = 1; j <= i; j++)
{
sum1 = sum1*j;
}
sum = sum + sum1;
sum1 = 1;
}
printf("sum = %22.15e\n", sum);
return 0;
}
#include
double sum1 = 0;
double sum2 = 0;
double sum3 = 0;
int main(void)
{
for (int i = 1; i <= 100; i++)
{
if(1 <= i && i <= 100)
sum1 = sum1 + i;
if (1 <= i && i <= 50)
sum2 = sum2 + i*i;
if (1 <= i && i <= 10)
sum3 = sum3 + 1.0/i;
}
printf("sum = %f", sum1 + sum2 + sum3);
return 0;
}
#include
#include
int sum = 0;
int main(void)
{
for (int i = 100; i <= 999; i++)
{
sum = (int)pow(i / 100, 3) + (int)pow(i / 10 % 10, 3) + (int)pow(i % 10, 3);
if (sum == i)
printf("i = %d\n", i);
}
return 0;
}
#include
#include
int sum = 0;
int i = 0, j = 0, k = 0;
int main(void)
{
for (int count = 100; count <= 999; count++)
{
i = count / 100;
j = count / 10 % 10;
k = count % 10;
sum = i * i * i + j * j * j + k * k * k;
if (sum == count)
printf("count = %d\n", count);
}
return 0;
}
#include
int sum = 0;
int i = 0, j = 0;
int main(void)
{
for (i = 1; i <= 1000; i++)
{
sum = 0;
for (j = 1; j < i; j++)
{
if (i % j == 0)
sum = sum + j;
}
if (sum == i)
printf("i = %d\n", i);
}
return 0;
}
#include
double sum = 0;
double a1 = 1, b1 = 2;
double bottle = 0;
int main(void)
{
for (int i = 1; i <= 20; i++)
{
sum = sum + b1 / a1;
bottle = b1;
b1 = a1 + b1;
a1 = bottle;
}
printf("sum = %16.10f", sum);
return 0;
}
#include
double height = 100;
double sum = 0;
int main(void)
{
for (int i = 1; i <= 10; i++)
{
if(i==1)
sum = sum + height;
else
sum = sum + 2*height;
height = height * 0.5;
}
printf("sum = %f,height = %f\n", sum, height);
return 0;
}
#include
int sum = 1;
int main(void)
{
for (int i = 1; i <= 9; i++)
{
sum = (sum + 1) * 2;
}
printf("sum = %d\n", sum);
return 0;
}
#include
#include
double x0 = 0, x1 = 0;
double a = 0;
int main(void)
{
printf("please type a value:");
scanf("%lf", &a);
x0 = a / 2;
x1 = 0.5 * (x0 + a / x0);
while (fabs(x1 - x0) >= 1e-5)
{
x0 = x1;
x1 = 0.5 * (x0 + a / x0);
}
printf("the square root of %f is %f ", a, x1);
return 0;
}
习题5-14
下面是牛顿迭代法的公式。注意,本题做出的前提是理解牛顿迭代法。
#include
#include
double x1=0, x0 = 0, f = 0, f1 = 0;
int main(void)
{
x1 = 1.5;
do
{
x0 = x1;
f = 2 * x0 * x0 * x0 - 4 * x0 * x0 + 3 * x0 - 6;
f1 = 6 * x0 * x0 - 8 * x0 + 3;
x1 = x0 - f / f1;
} while (fabs(x1 - x0) >= 1e-5);
printf("the root of equation is %f\n", x1);
return 0;
}
习题5-15 略~~
#include
int n = 0;
int len = 0;
int main(void)
{
printf("-------------------------\n");
printf("\
* \n\
*** \n\
***** \n\
*******\n\
***** \n\
*** \n\
* \n");
printf("-------------------------\n");
printf("please type n value:"); //for example n = 7 len = 3
scanf("%d", &n); //for example n = 9 len = 4
len = n / 2;
for (int i = 1; i <= 2*len+1; i++)
{
if (i <= len)
{
for (int j = 1; j <= len - i + 1; j++)
putchar(' ');
for (int j = 1; j <= n - (len - i + 1) * 2; j++)
putchar('*');
for (int j = 1; j <= len - i + 1; j++)
putchar(' ');
putchar('\n');
}
else if (i == len + 1)
{
for (int j = 1; j <= n; j++)
putchar('*');
putchar('\n');
}
else
{
for (int j = 1; j <= i-len-1; j++)
putchar(' ');
for (int j = 1; j <= n - (i - len - 1) * 2; j++)
putchar('*');
for (int j = 1; j <= i - len - 1; j++)
putchar(' ');
putchar('\n');
}
}
return 0;
}