mdsk.net
当前位置:首页 >> 编写程序,输入一个正整数,计算并显示该整数的各... >>

编写程序,输入一个正整数,计算并显示该整数的各...

#include int main() { int x,sum=0,t; printf("请输入一个正整数:"); scanf("%d",&x); while(x!=0) { t=x%10; x=x/10; sum+=t; } printf("该数的各位数字之和:%d",sum); }

#include void main(){int n,m=0,s=0;scanf("%d",&n);while(n){m=m*10+n%10;n/=10;}while(m){printf("%d+",m%10);s+=m%10;m/=10;}printf("\b=%d",s);}//运行示例:

#include int main() { int x;//那个整数 int yu;//x除以10的余数 int sum=0; scanf("%d",&x); while(x>0) { yu=x%10; sum+=yu; x/=10; } printf("%d",sum); return 0; }

如果只需要求和,那么: #include void main(){int n,s=0;scanf("%d",&n);while(n){s+=n%10;n/=10;}printf("%d",s);}运行示例: 如果需要列出算式,那么: #include void main(){int n,k=0,m=0,s=0;scanf("%d",&n);while(n){m=m*10+n%10;s+=n%10...

#include int main() { int a, b, i, t; int k = 0, sum = 0; scanf("%d", &a); //输入整数a t = a; if(a / 10 == 0) { k = 1; sum = a; printf("%d\n", k); printf("%d\n", sum); } else { while(a / 10 != 0) { a = a / 10; //每除10,就有一...

#include void main() { int i,n; scanf("%d",&n); for ( i=0;i

#include int main() { int a, sum = 0; scanf("%d", &a); while (a) { sum += a % 10; a /= 10; } printf("sum = %d\n", sum); return 0; }

以下程序能满足你的要求,包括逆序输出各个位数: /* **获取数字num的长度 */ int getLength(int num) { int i = 1; int temp = num; while(temp/10 != 0) { ++i; temp /= 10; } return i; } /* **顺序输出数字num的各个位...

#include void main() { int n,sum=0; scanf("%d",&n); while (n) { sum +=n%10; n /=10; } printf("%d",sum); }

Sub xx() x = InputBox("请输入一个正整数:") For i = 1 To Len(x) n = n + Val(Mid(x, i, 1)) Next Debug.Print nEnd Sub

网站首页 | 网站地图
All rights reserved Powered by www.mdsk.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com