设计一个程序,输入一个四位数,求出各个数字之和
答案:4 悬赏:30
解决时间 2021-02-21 17:48
- 提问者网友:饮鸿
- 2021-02-21 00:37
设计一个程序,输入一个四位数,求出各个数字之和
最佳答案
- 二级知识专家网友:何以畏孤独
- 2021-02-21 01:06
#include<stdio.h>
main()
{
int a,b ,c,d,sum,tem;
printf("Please input a number:\n");
scanf("%d",&tem);
if(tem>=1000&&tem<=9999)
{
a=temp%10; //个位
b=temp/10%10;//十位
c=temp/100%10;//百位
d=temp/1000;//千位
sum=a+b+c+d;
printf("the result is %d",sum);
}else
{
printf("Error");
}
}
main()
{
int a,b ,c,d,sum,tem;
printf("Please input a number:\n");
scanf("%d",&tem);
if(tem>=1000&&tem<=9999)
{
a=temp%10; //个位
b=temp/10%10;//十位
c=temp/100%10;//百位
d=temp/1000;//千位
sum=a+b+c+d;
printf("the result is %d",sum);
}else
{
printf("Error");
}
}
全部回答
- 1楼网友:万千宠爱
- 2021-02-21 04:10
有语言要求吗?
- 2楼网友:魅世女王
- 2021-02-21 03:04
procedure test(p_input_amount in number
, p_output_amt out number)
is
v_len number;
v_amount number;
begin
v_amount := 0;
v_len := length(p_input_amount );
if v_len = 4 then
for i in 1..v_len loop
v_amount := v_amount + mod(p_input_amount ,10);
p_input_amount := trunc(p_input_amount /10);
end loop;
else
null; ---自己处理特殊数据
end if;
p_output_amt := v_amount;
exception
when others then
null; --处理异常。
end;
- 3楼网友:荒唐后生
- 2021-02-21 02:26
# include <stdio>
int main (void)
{
int val;
int sum=0;
printf ("请输入任意一个四位数: ");
scanf ("val=%d",&s);
m=val;
while(m>o)
{
sum=sum+m%10;
m=m/10;
}
printf ("sum=%d",sum);
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
• 手机登qq时,显示手机磁盘不足,清理后重新登 |
• 刺客的套装怎么选啊? |