用C语言编写一个程序实现输入一个五位数得到各位上数字的和,比如属如“12345”输出 1+2+3+4+5=15

如果不限制到五位数 是随意输入一个数的话好编写吗?

#include "stdio.h"
#include <conio.h>
void main()
{
int n,s=0;
printf("please enter a integer between 1 and 100000:");
scanf("%d",&n); /*这儿可以加一个判断语句看是不是五位 n>99999?
s=n/10000+(n%10000)/1000+(n%1000)/100+(n%100)/10+n%10;
printf("\nresult:%d",s);
getch();
}

//嗯,下面这个是取巧的,可以随便输入
#include "stdio.h"
#include <conio.h>
void main()
{
int s=0;
char c;
printf("please input a number:");
c=getch();
while(c!=13)
{
printf("%c",c);
s=s+(int)c-48;
c=getch();
}
printf("\nresult:%d",s);
getch();
}
//可以自己加判断语句 看输入的c是不是数字 c<'0'||c>'9'
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-02-20
还好了,用除10取余的思路吧,详细q我509967604
相似回答