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.
#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;
}
Comments
Leave a comment