| | |
Adding digits of a number together
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 40
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 Nov 4th, 2009
>> 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.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle. 2) Problem 2[b]solved by : jonsca
![]() |
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
Views: 309 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






