I want to write a short code to sum the digits of a 5digit number. Someone please help me to developed the pseudo code and maybe the approach to write to code. Thanks.
I want to write a short code to sum the digits of a 5digit number. Someone please help me to developed the pseudo code and maybe the approach to write to code. Thanks.
What ideas do you have?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You can do something like:
using the mod operator you take the last digit of the number and decrement the number dividing it by 10Code:while(n > 0) sum += n%10; // sum is 0 at the beginning n /= 10;
Do modulus ten on the number to get the last digit, then divide by ten (C rounds down), and repeat.
Looks like rob90 beat me.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I was looking at an array approach...what do you think?
Thanks rob90 but I was hoping for some rather simple approach to it..like an array approach. Help please..
If you read in the number as a string, that certainly is a good idea, otherwise you might as well use what has been suggested.Originally Posted by KOFI
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes, you should take the number as a string and then read each element of the array and convert it to an integer to make the sum, but I think that using the modulus is even simpler
I'm gonna third the opinion that modulus/division is the simplest method, but if you want to use an array:
[edit] corrected from sscanf to sprintf
"array" is now a string version of x, where each character is a single digit. You will now need to consult the ASCII table to do arithmetic with it:Code:int x = 54321; char array[6]; // we need room for the null terminator ssprintf(array, "%d", x);
ASCII Table / Extended ASCII Codes
Which is not a bad idea, ASCII is very fundamental to even basic C programming. Feel free to ask questions.
Last edited by MK27; 04-01-2010 at 03:33 PM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Luckily, you don't: you can just subtract '0'.Originally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Because it is guaranteed that for any standard conforming implementation, the sequence of digits in the character set from '0' to '9' will be contiguous.Originally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I still think KOFI will be missing out on the excitement, unless...
ASCII Table / Extended ASCII Codes
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I think it's sprintf()
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge