该程序工作正常,直到10位数,但如果我使用11位数字,它给出0作为输出。 [英] The program is working fine uptill 10 digits but if I use a number of 11 digits it is giving 0 as output.
问题描述
main()
{
int num;
printf(输入数字: );
scanf(%d,& num);
int digit,sum = 0;
while(num> = 1)
{
digit = num%10;
sum = sum + digit;
num = num / 10;
}
printf(\ nSum of digits:%d,res);
}
我尝试了什么:
我在网上查了另一种方法,但我的方法没有任何错误。请检查
引用:程序工作正常截止到10数字,但如果我使用11位数字,它给出0作为输出。
因为int是有限的,C语言有超过1个整数类型,以满足不同的需求。 />
你必须学习 C数据类型并选择适合你需要的那个。
答案取决于你想要处理多少位数。 / BLOCKQUOTE>
main()
{
int num;
printf("Enter a number:");
scanf("%d",&num);
int digit,sum=0;
while(num>=1)
{
digit = num%10;
sum=sum+digit;
num = num/10;
}
printf("\nSum of digits: %d",res);
}
What I have tried:
I checked online for another method but i am not getting any error in my approach. Please check.解决方案Quote:The program is working fine up till 10 digits but if I use a number of 11 digits it is giving 0 as output.
Because an int is limited, C language have more than 1 integer type to accommodate different needs.
you have to learn the C datatypes and choose the one that fit your needs.
The answer depend on how many digits you want to handle.
这篇关于该程序工作正常,直到10位数,但如果我使用11位数字,它给出0作为输出。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!