Enter any positive integer from the keyboard, find its digits, and output the sum of each digit.

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;
}

result:

Summarize


For example: The above is what I will talk about today. This article only briefly introduces C++ to find the number of digits and the sum of the digits of an integer.

Guess you like

Origin blog.csdn.net/m0_65420451/article/details/125232951