//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<string>
#include<vector>
#include <sstream>
usingnamespace std;
int main()
{
int integer = 0;
int sum = 0;
int digits = 0;
string value;
cout << "Please enter any number: ";
cin >> integer;
cout << " You entered: " << integer << endl;
string value = to_string(integer);
sum = digits + digits;
cout << " The sum of these digits is:" << digits;
return 0;
}
int main()
{
int integer = 0;
int sum = 0;
int digits = 0;
string value;
char ch;
cout << "Please enter any number: ";
cin >> integer;
//Convert int into a string ( this syntax makes no fucking sense)
stringstream ss;
ss << integer;
string str = ss.str();
// It actually worked for once, except it did not out put it with space
// so technically it does not work
cout << " You entered: " << str << endl;
sum = digits + digits;
cout << " The sum of these digits is:" << digits;
Hey a lot of the code seems rather unnecessary, this is how you could script your variables to get a sum, then print.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <stdio.h>
usingnamespace std;
int main()
{
int a, b;
cout<<"a + b = sum total"<<endl;
cout<<"Enter value of 'a'"<<endl;
cin>>a;
cout<<"Enter value of 'b'"<<endl;
cin>>b;
int sumtotal = a + b;
cout<<a<<" + "<<b<<" = "<<sumtotal<<endl;
return 0;
}
Hope this helped bud :)
end if you would like to be able to input decimal/negative values, change
//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 <limits>
#include <string>
void printDigitByDigit(const std::string& str);
void waitForEnter();
int main()
{
std::cout << "Could you please give me a number? ";
std::string first;
std::getline(std::cin, first);
std::cout << "Could you please give me another number? ";
std::string second;
std::getline(std::cin, second);
std::cout << "The first number you gave me was: ";
printDigitByDigit(first);
std::cout << "The second number you gave me was: ";
printDigitByDigit(second);
std::cout << "Their sum is " << std::stoi(first) + std::stoi(second) << '\n';
waitForEnter();
return 0;
}
void printDigitByDigit(const std::string& str)
{
for(size_t i=0; i<str.length(); i++) {
std::cout << str.at(i);
if(i == str.length() - 1) { std::cout << '\n'; }
else { std::cout << ' '; }
}
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
Could you please give me a number? 84512351
Could you please give me another number? -23154265
The first number you gave me was: 8 4 5 1 2 3 5 1
The second number you gave me was: - 2 3 1 5 4 2 6 5
Their sum is 61358086
Press ENTER to continue...
You don't have to use strings to sum the digits of an integer. You can extract the one's digit with the modulus operator(%) and then divide the number by 10. Keep repeating this process until the number is 0.
1 2 3 4 5 6 7 8 9
int sum_digits( int n )
{
int sum{};
while( n != 0 ) {
sum += n % 10;
n /= 10;
}
return sum;
}
For example, say we have the number 574.
574 % 10 is 4. sum += 4 -> sum is 4. 574 / 10 is 57 (integer division).
57 % 10 is 7. sum += 7 -> sum is 11. 57 / 10 is 5.
5 % 10 is 5. sum += 5 -> sum is 16. 5 / 10 is 0.
Loop terminates and we return 16.
#include <iostream>
usingnamespace std;
int main()
{
int N;
cout << "Input N: ";
cin >> N;
int base = 1;
while ( N >= base * 10 ) base *= 10;
int sum = 0;
while ( base >= 1 )
{
int digit = N / base;
cout << digit << " ";
sum += digit;
N -= digit * base;
base /= 10;
}
cout << "\nSum of digits = " << sum;
}