| | |
Adding digits of a number together
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 29
Reputation:
Solved Threads: 0
I'm trying to make a function that adds the digits of a number together and finds the sum of them.
For example, for the number 9211, I want to add 9 + 2 + 1 + 1 and output the sum.
Here's my function, I can't figure out where I am going wrong.
Could someone point out why the result isn't coming out correctly?
For example, for the number 9211, I want to add 9 + 2 + 1 + 1 and output the sum.
Here's my function, I can't figure out where I am going wrong.
Could someone point out why the result isn't coming out correctly?
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int SumFunction (int x) { int result = 0; result = ((x % 10) + ((x / 10) % 10) + ((x / 100) % 10) + ((x / 1000) % 10) + ((x / 10000) % 10)); return (result); }
0
#7 23 Days Ago
>> result = ((x % 10) + ((x / 10) % 10) + ((x / 100) % 10) + ((x / 1000) % 10) + ((x / 10000) % 10));
You know your formula won't handle number above 99999, try
doing this with for loops or recursions.
You know your formula won't handle number above 99999, try
doing this with for loops or recursions.
I give up!
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?![]() |
Similar Threads
- The sum of digits of a number (C)
- Ignoring certain digits of a number (C++)
- number of digits in a base (C)
- Finding the number of digits in a number (C++)
- Help with Linked Lists with digits (C++)
- Program that takes an integer value and returns the number with digits reversed (C++)
Other Threads in the C++ Forum
- Previous Thread: matrix-chain multiplication
- Next Thread: plzzz help me with do while loops
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






