answersLogoWhite

0

#include
#include

using std::cin;
using std::cout;
using std::endl;
using std::string

int main()
{
const int numberOfdigits = 5;
string myNumber = "0";
char myNumberChar[numberOfdigits] = {0};
cout << endl << "Enter 4 digit integer: ";
cin >> myNumber;

int sumOfDigits = 0;
int temp = 0;
for (int arrayIndex = 0; arrayIndex < (numberOfdigits - 1); arrayIndex++)
{
temp = atoi(&myNumber.substr(arrayIndex, 1)[0]);
sumOfDigits += temp;
}
cout << endl << "Sum of 4 digits is: " << sumOfDigits << endl;


system("PAUSE");
return 0;

}

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

JordanJordan
Looking for a career mentor? I've seen my fair share of shake-ups.
Chat with Jordan
MaxineMaxine
I respect you enough to keep it real.
Chat with Maxine
FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran

Add your answer:

Earn +20 pts
Q: Sum of 4digit of a number in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp