Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.
topic:
Enter any positive integer from the keyboard, find its digits, and output the sum of each digit.
Code:
#include<iostream>
using namespace std;
int main()
{
int n,i=0,sum=0;
cout<<"请输入一个整数n"<<endl;
cin>>n;
while(n!=0)
{
sum=sum+n%10;
n=n/10;
i++;
}
cout<<"输出n的位数为i="<<i<<endl;
cout<<"输出位数和为sum=" <<sum<<endl;
return 0;
}