mdsk.net
当前位置:首页 >> 计算整数数字的和 >>

计算整数数字的和

#include <stdio.h> int main() { int sum = 0; int i, t, j; scanf ("%d", &t); for ( ; t; t /= 10 ) sum += t % 10; printf ("%d\n", sum); getchar(); getchar(); return 0; }

# include"stdio.h" void main() { int i=10,k=0 ,sum=0,a; scanf("%d",&a); //输入一个数 if(asum=a; else{do {k=a%i; //取数的最后一位 sum+=k; //求和 a=a/10;} //把最后一位去掉 while(a>=10); //当条件成立循环上面的语句 sum+=a;} //条件不成立执行这条语句 printf("%d",sum); } 希望对你有一定帮助!!

#include<stdio.h> int sum_num(int num)//你要的函数 { int sum=0; while(num!=0) { sum+=num%10; num/=10; } return sum; } void main(){ int sum=0,num; printf("请输入你的数:"); scanf("%d",&num); sum=sum_num(num); printf("%d\n",sum); }

/* 编写一个程序,计算任意输入的整数的各位数字之和*/#include "stdio.h" void main() { int n,sum=0; printf("请输入一个整数:"); scanf("%d",&n); while(n) { sum=sum+n%10; n=n/10; } printf("输入的整数的各位数字之和为%d",sum); }

int sum(int x) { int sum=0,n; do{ n=x%10; sum=sum+n; x=x/10; }while(x>0); return(sum); } 不能x>=0,那样无穷循环了 ,只要>0就进入循环, 关键错误 while(x>0);是对的

#include<stdio.h> int Sum(int n); void main() { int n=0; do{ printf("请输入一个正整数:"); fflush(stdin); scanf("%d",&n); }while(n<0); printf("%d各个位数字之和为:%d\n",n,Sum(n)); } int Sum(int n) { int sum=0; while(n != 0) { sum += n%10; n /= 10; } return sum; }

min=&quotprivate sub command1_click() dim i as long, j(50) as long, max as long; & min print "min位置"max位置" for i = 1 to 50 if j(i) = max then print i next print " max then max = j(i) if j(i) < min then min = j(i) next print "max=" & max print &

用C来为你做这个程序.#include int main() { int NUM = 2568; //你设定的正整数 int tmp1,tmp2,idx=0; int result = 0; tmp1 = NUM; while(tmp1> 0) { tmp2 = tmp1%10; result = result + tmp2; //各位相加 tmp1 = (int)(tmp1 /10); //除去最未末位 } printf("The NUM is %d, and the result is %d\n",NUM,result); return 0; }

public class A { public int sum(int number) { String a = number + ""; int sum = 0; int length = a.length(); String[] aArray = new String[length]; for (int i=0; i<length; i++) { aArray[i] = a.substring(i, i+1); } for (int i = 0; i < length; i++) { sum += Integer.

#include<stdio.h> int main() {int a[100],b[100]={0},i,n; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); for(;a[i];a[i]/=10) b[i]+=a[i]%10; } printf("%d个数字各位数之和:\n",n); for(i=0;i<n;i++) printf("%d ",b[i]); printf("\n"); return 0; }

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