对一个n位数求和,如12345,求每位数字之和,应该输出15
//求和的经典操作:
// 1. 定义基本sum = 0,在num!=0之前做如下循环:
// 2. 先取低位值相加,即sum = sum + num%10
// 3. num右移动,即 num = num/10
public void getSum (int num){
int sum = 0;
while (num !=0){
sum += num%10;
num /=10;
}
System.out.println(sum);
}
标签:10,每位,code,数字,int,sum,num
From: https://www.cnblogs.com/xiaoyu-jane/p/16724949.html