1、求出0~999之间的所有“水仙花数”并输出。
“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,如;153=1+5+3?,则153是一个“水仙花数”。
在数论中,水仙花数(Narcissistic number)也称为自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),是指一N位数,其各个数之N次方和等于该数。
例如153、370、371及407就是三位数的水仙花数,其各个数之立方和等于该数:
153 = 1^3 + 5^3 + 3^3。 370 = 3^3 + 7^3 + 0^3。 371 = 3^3 + 7^3 + 1^3。 407 = 4^3 + 0^3 + 7^3。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c;
int i, sum;
for (i = 100;i < 999;i++)
{
a = i / 100;
b = (i % 100) / 10;
c = i % 10;
sum = a * a*a + b * b*b + c * c*c;
if (i == sum)
{
printf("%d ", i);
}
}
printf("\n");
system("pause");
return 0;
}
2、求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字, 例如:2+22+222+2222+22222
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int num = 0;
int sum = 0;
printf("input a number:\n");
scanf("%d", &num);
int Sn = num;
for (i = 0;i < 5;i++)
{
printf("%d+", Sn);
sum += Sn;
Sn = Sn * 10 + num;
}
printf("\b=%d\n", sum);
system("pause");
return 0;
}
3、编写一个程序,可以一直接收键盘字符, 如果是小写字符就输出对应的大写字符, 如果接收的是大写字符,就输出对应的小写字符, 如果是数字不输出。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ch = 0;
printf("请输入字符:>\n");
while ((ch = getchar()) != EOF)
{
if (ch >= 'a'&&ch <= 'z')
printf("%c", ch - 32);
else if (ch >= 'A'&&ch <= 'Z')
printf("%c", ch + 32);
else if (ch >= '0'&&ch <= '9')
;
else
;
}
system("pause");
return 0;
}