Answer to Question #100881 in C++ for Zohaib

Question #100881
Write a program that asks the user to enter a four-digit number, and then prints the
sum of its digits. A session with the program should have the following appearance:
Sample Output:
Enter a three-digit number: 1283
The sum is: 14
Please solve this problem on dev c++
1
Expert's answer
2020-01-03T03:11:13-0500

#include <iostream>

#include <string>

using namespace std;


int main(int argc, char const *argv[])

{

cout << "Enter a three-digit number: ";

string input;

cin >> input;

int sum = 0;

for (int i = 0; i < input.length(); ++i)

{

sum += (int)input[i] - 48;

}

cout << "The sum is: " << sum << endl;

return 0;

}

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