1题
请阅读下面的程序,在横线处填写正确的代码,该程序的功能是:求1-10的奇数和。
#include <stdio.h>
int main()
{
int x, s;
s=0;
for (x=1; x<=10; _______)
{
}
printf("奇数和为:%d", s);
return 0;
}
2题
请阅读下面的程序,分析程序是否能编绎通过并正确运行,如果不能,说明原因;如果能,请写出运行结果。
#include <stdio.h>
int main()
{
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= i; j++)
{
printf("(%d,%d)\n", i, j);
}
}
return 0;
}
3题
编写程序,打印出所有的水仙花数。所谓水仙花数是指一个三位数,其各位数字立方和等于该数本身。例如153是一个水仙花数。
提示:
1)先把一个数的每个位上的数字分离出来
2)所有的三位数,可以用循环语句去遍历
4题
编写程序,打印出九九乘法表;
提示:
每一行的列数都不相同,可以用循环嵌套
1.
#include <stdio.h>
int main()
{
int x, s;
s = 0;
for (x = 1; x <= 10;x += 2)
{
s += x;
}
printf("奇数和为:%d", s);
return 0;
}
2.不能,C语言不支持在for循环中初始化变量
3.
#include <stdio.h>
int main()
{
int hun, ten, ind, n;
for( n=100; n<1000; n++ ) /*整数的取值范围*/
{
hun = n / 100;
ten = (n-hun*100) / 10;
ind = n % 10;
if(n == hun*hun*hun + ten*ten*ten + ind*ind*ind) /*各位上的立方和是否与原数n相等*/
printf("%d ", n);
}
printf("\n");
return 0;
}
4.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num1, num2;
for (num1 = 1; num1 <= 9; num1++)
{
for (num2 = 1; num2 <= num1; num2++)
{
printf("%d*%d=%-3d", num1, num2, num1*num2);
}
printf("\n");
}
system("pause");
return 0;
}