Answer to Question #327781 in C++ for nana

Question #327781

Write a program that prompts the user to input an integer and then outputs both



the individual digits of the number and the sum of the digits.




1
Expert's answer
2022-04-12T13:26:27-0400
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
	int number;
	vector<int>v;
	cout << "Please, enter a number: ";
	cin >> number;
	double sum = 0;
	cout << "Individual digits: ";
	while (number > 0)
	{
		int tmp = number % 10;
		sum += tmp;
		v.push_back(tmp);
		number /= 10;
	}
	reverse(v.begin(), v.end());
	vector<int>::iterator it;
	for (it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << "\nThe sum of all digits is " << sum;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS