关于#c++#的问题,如何解决?

一行一个正整数,不知道有几位数,反正不超过int,需要用到循坏。比较麻烦。

img

这题我会代码马上发你
代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    int ans=0;
    while(n)
    {
        ans+=n%10;
        n/=10;
     } 
     cout<<ans<<endl;
    return 0;
}