code4sk/c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第1次月考/7-4 求整数的位数及各位数字之和.cpp
2023-10-18 16:17:32 +08:00

30 lines
431 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
7-4 求整数的位数及各位数字之和
分数 10
作者 C课程组
单位 浙江大学
对于给定的正整数N求它的位数及其各位数字之和。
输入格式:
输入在一行中给出一个不超过10^9的正整数N。
输出格式:
在一行中输出N的位数及其各位数字之和中间用一个空格隔开。
输入样例:
321
输出样例:
3 6
*/
#include<stdio.h>
int main(){
int N,cnt=0,sum=0;
scanf("%d",&N);
for(;N!=0;){
sum+=N%10;
N=N/10;
cnt++;
}
printf("%d %d",cnt,sum);
return 0;
}