【发布时间】:2019-03-24 04:45:09
【问题描述】:
我需要找到一个五位数的数字之和。比如3709的数字之和就是3 + 7 + 0 + 9 = 19。
#include <stdio.h>
int main()
{
int sum;
char digit_1, digit_2, digit_3, digit_4, digit_5;
printf("Plase enter a five digit number\n");
scanf("%c,%c,%c,%c,%c", &digit_1, &digit_2, &digit_3, &digit_4, &digit_5);
sum = digit_1 + digit_2 + digit_3 + digit_4 + digit_5;
printf("the sum of the digits is: %d", sum);
return 0;
}
输出:
plase enter a five digit number
3709
the sum of the digits is 51
由于某种原因,它没有显示正确答案,我似乎找不到什么问题。
【问题讨论】:
-
你的代码在哪里?
-
将您的代码作为文本发布在问题中。
-
你正在使用
char类型的数据而不是int -
作为一般规则,请避免在问题中张贴文字图片。如果可能,最好复制原始文本。
-
@suvojit_007 很好,问题是他添加的是 ascii 值而不是十进制。