5.
下面代码的输出结果:
#include<stdio.h>
int main()
{
int k, x;
for (k = 0, x = 0; k <= 9 && x != 10; k++)
x = x + 2;
printf("%d,%d", k, x);
return 0;
}
6.
程序执行时输入12345#的结果是
(14)
从键盘上输入ABCDEFG↙输出结果是:
#include<stdio.h>
int main()
{
char ch;
for (; ch = getchar() != '\n';)
printf("%c", ch);
return 0;
}
(15)
无限循环语句
#include<stdio.h>
int main()
{
int i, a = 1;
unsigned j;
for (j = 0; j > 15; j += 2)a++;
printf("%d", a);
return 0;
}
(1)
运行结果
#include<stdio.h>
int main()
{
int i;
for (i = 1; i < 6; i++)
{
if (i % 2)
printf("*");
else
printf("#");
}
return 0;
}
(2)
运行结果
#include<stdio.h>
int main()
{
int m = 1, n, i;
for (i = 0; i < 5; i++)
{
int m = 1;
m++;
if (i == 4)n = m;
}
printf("%d%d", m, n);
return 0;
}
(3)
运行结果
#include<stdio.h>
int main()
{
int i;
for (i = 'a'; i < 'f'; i++, i++)
printf("%c", i - 'a' + 'A');
return 0;
}
(4)
运行结果
#include<stdio.h>
int main()
{
int f, f1, f2, i;
f1 = 1, f2 = 2;
printf("%2d%2d", f1, f2);
for (i = 3; i <= 5; i++)
{
f = f1 + f2;
printf("%2d", f);
f1 = f2; f2 = f;
}
return 0;
}
(5)
运行结果
#include<stdio.h>
int main()
{
int n = 5;
do
{
switch (n % 2)
{
case 0:n--; break;
case 1:n--; continue;
}
n--;
printf("%2d", n);
} while (n > 0);
return 0;
}
(7)
运行结果
#include<stdio.h>
int main()
{
int x = 10;
while (x--);
printf("x=%d\n", x);
return 0;
}
(9)
运行结果
#include<stdio.h>
int main()
{
int m = 9;
for (; m > 0; m--)
if (m % 3 == 0)
printf("%d", --m);
return 0;
(10)
运行结果
#include<stdio.h>
int main()
{
int m = 9;
for (; m > 0; m--)
if (m % 3 == 0)
printf("%d", --m);
return 0;
(11)
#include<stdio.h>
int main()
{
int x = 3;
do
{
printf("%3d", x = x - 3);
} while (!x);
return 0;
}
(12)
#include<stdio.h>
int main()
{
int x = 2;
do
{
printf("%3d", !x - 2);
} while (--x);
return 0;
}
(13)
#include<stdio.h>
int main()
{
int n = 12345, d;
while (n != 0)
{
d = n % 10;
printf("%d", d);
n = n / 10;
}
return 0;
}
(14)
#include<stdio.h>
int main()
{
int m = 0, sum = 0;
char c, oldc = '+';
do
{
c = getchar();
if (c <= '9' && c >= '0')m = 10 * m + c - '0';
else
{
if (oldc == '+')sum += m;
else sum -= m;
m = 0;
oldc = c;
printf("%3d", sum);
}
} while (c != '=');
return 0;
}
(16)
#include<stdio.h>
int main()
{
int t = 1, n = 235;
do
{
t *= n % 10;
n /= 10;
} while (n);
printf("%d\n", t);
return 0;
}
(17)
#include<stdio.h>
int main()
{
int m = 5, n = 0;
while (m > 0)
{
switch (m)
{
case 1:
case 3:n = +1; m--; break;
default:n = 0; m--;
case 2:
case 4:n = +2; m--; break;
}
printf("%2d", n);
}
return 0;
}
(18)
#include<stdio.h>
int main()
{
int i, m = 0;
for (i = 0; i < 5; i++)
{
switch (i)
{
case 0:
case 1:m++;
case 3:m++;
case 4:m--; break;
}
}
printf("%d\n", m);
return 0;
}
(19)
#include<stdio.h>
int main()
{
int i, b = 0, c = 2;
for (i = 0; i < 2; i++)
switch (++b, b * c)
{
case 1:printf("1");
case 2:printf("2");
case 3:printf("3"); break;
default:printf("other\n");
}
return 0;
}
四
(1)
#include<stdio.h>
int main()
{
int denominator, flag, i, n;
double item, sum;
printf("Please input 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 += 2;
}
printf("sum=%f\n", sum);
return 0;
}
(2)
#include<stdio.h>
int main()
{
int i, n;
float sum = 0, flag = 1.0;
scanf_s("%d", &n);
for (i = 1; i <= n; i++)
{
sum = sum + flag * 1 / (2 * i - 1);
flag = -flag;
}
printf("sum=%f\n", sum);
return 0;
}
(3)
#include<stdio.h>
int main()
{
int n, s;
printf("Enter a number:");
scanf_s("%d", &n);
printf("Output:");
do
{
s = n % 10;
printf("%d", s);
n /= 10;
} while (n != 0);
return 0;
}
(4)
#include<stdio.h>
int main()
{
int n = 0, s = 1.0, k = 1;
while (k <= n)
{
s = s + 1.0 / (k * (k + 1));
k++;
}
printf("s=%f\n\n", s);
return 0;
}
(10)
#include<stdio.h>
int main()
{
int i;
for (i = 100; i < 200; i++)
if ((i - 2) % 4 == 0)
if (!((i - 3) % 7))
if ((i - 5) % 9 == 0)
printf("%d", i);
return 0;
}
(11)
#include<stdio.h>
int main(void)
{
int count, i, n;
double grade, total;
printf("Enter n:");
scanf_s("%d", &n);
total = 0;
count = 0;
for (i = 1; i <= n; i++)
{
printf("Enter grade #%d:", i);
scanf_s("%lf", &grade);
total = total + grade;
if (grade < 60)
count++;
}
printf("Grade average=%.2f\n", total / n);
printf("Number of failures=%d\n", count);
return 0;
}
(12)
#include<stdio.h>
int main()
{
int i, j;
for (i = 0; i < 10; i++)
{
j = i * 10 + 6;
if (j % 3 == 0)
printf("%3d", j);
}
return 0;
}