C++ Programming Integer Sum troubles

my question is this.

A problem that lets the user enter any positive integer, but you do not have to check for this,
and then calculates the sum of the digits and displays this to user. For example, if user enters
14503, the outputted sum of the digits would be 13.

here us my program

#include <iostream>
int main(void)
{
int integer, sum=0;
while(integer)
{
sum += integer % 10;
integer /= 10;
}
cout << "The sum of the integers numbers is " <<sum;
return 0;
}
Last edited on
Well, you forgot to take number from user, but aside from that your program looks fine.
#include <iostream>
int main(void)

{
int Integer, Sum=0;
cout << "Enter an integer --> ";
cin >> Integer;
while(Integer>0)
{
Sum += Integer % 10;
Integer /= 10;
}
cout << "The sum of the integers numbers is " <<Sum;
return 0;
}

it tells me cout is undefined??
It is std::cout
nevermind i was missing

using namespace std;
thank you MiiNiPaa!
Topic archived. No new replies allowed.