• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

C++ programing

I want to write a program that displays the sum of the digits of any non-negative
interger. I going to give you two program runs are shown below:

Enter an integer: 145
sum of digits is: 10, becuase 1+4+5 = to 10, but I don't not how to write the program.

please helpme.
0
glennys
Asked:
glennys
  • 3
1 Solution
 
ozoCommented:
You could use / and % to split up the integer
0
 
arnondCommented:
do something like this:

#include <iostream.h>

int main()
{
int num,sum=0;

cout << "Enter number: " << endl;
cin >> num;

while (num > 0)
 {
  sum += num%10;
  num /=10;
 }

 cout << "sum is: " << sum << endl;
 return (0);
}


Arnon David.
0
 
glennysgCommented:
Veru good
0
 
arnondCommented:
if you like my answer, you can now accept and grade it.

Arnon David.
0
 
arnondCommented:
glennys, are you still here ?
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now