1.首先应该对其求各个位数之和
int getSum(int num){
int sum = 0;
while (num != 0){
int i = num % 10;
sum += i;
num= 10;
}
return sum;
}
2.求前n个数的各个位数之和
#include "stdafx.h"
#include<stdio.h>
int getSum(int num){
int sum = 0;
while (num>0){
int i = num % 10;
sum =sum+ i;
num=num/10;
}
return sum;
}
int main(){
int x;
scanf_s("%d", &x);
int sum, total = 0;
for (int i = 0; i <=x; i++){
sum = getSum(i);
total =total+ sum;
}
printf("%d", total);
return 0;
}