| | |
Even sum of digits
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Sep 2008
Posts: 90
Reputation:
Solved Threads: 12
I agree that the sum is different. I said so before. All I say is that the result of what we're interested in, in this case, remains the same - just because the ASCII value of '0' modulo 2 is 0.
We're not interested in the sum of the digits. We're interested to know if the number's sum of digits is even or odd, or in other words: if it's 0 (modulo 2) or 1 (modulo 2) respectively.
We're not interested in the sum of the digits. We're interested to know if the number's sum of digits is even or odd, or in other words: if it's 0 (modulo 2) or 1 (modulo 2) respectively.
Last edited by unbeatable0; Apr 10th, 2009 at 2:17 pm.
Guys instead of USING AN ARPHODOX method to solve your conversion from char to numerical. Why dont you use the atoi() function built in. That way you will not need to care abt any format.
c++ Syntax (Toggle Plain Text)
string a; unsigned short int Sum=0,i; a="12522"; for(i=0;i<a.length();i++) { string b; b=a[i]; Sum+=atoi(b.c_str())]; }
•
•
•
•
Guys instead of USING AN ARPHODOX method to solve your conversion from char to numerical. Why dont you use the atoi() function built in. That way you will not need to care abt any format.
c++ Syntax (Toggle Plain Text)
string a; unsigned short int Sum=0,i; a="12522"; for(i=0;i<a.length();i++) { string b; b=a[i]; Sum+=atoi(b.c_str())]; }
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Agreed, but that's logic: 48 modulo 2 is equal to 0 ...
Agreed.
You'll have to explain this carefully using an example ...
•
•
•
•
We're interested to know if the number's sum of digits is even or odd
You'll have to explain this carefully using an example ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
Agreed, but that's logic: 48 modulo 2 is equal to 0 ...
Agreed.
You'll have to explain this carefully using an example ...
C++ Syntax (Toggle Plain Text)
48%2=0;
Unbeatable wants
C++ Syntax (Toggle Plain Text)
48, 4+8=12;//SUM OF DIGITS 12%2=0// even
Agreed with what you're saying, I also want that, but my problem is that Unbeatable explained this in a very strange way ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Mar 2009
Posts: 181
Reputation:
Solved Threads: 28
I thought I would throw another solution using recursion out there. Not fully tested but should do the job.
cpp Syntax (Toggle Plain Text)
int sum_digits(int number) { int remainder = number%10; if (remainder > 0) return remainder+sum_digits((number-remainder)/10); else if (number >= 10 && remainder == 0) return sum_digits(number/10); return number; } bool sum_digits_even(int number) { return !(sum_digits(number) % 2); }

c++ Syntax (Toggle Plain Text)
bool isEven(const char* number) { bool even = false; if (number) { if (*number == '-' || *number == '+') number++; if (*number) { even = true; do switch (*number++) { case '1': case '3': case '5': case '7': case '9': even = !even; continue; case '0': case '2': case '4': case '6': case '8': continue; default: return false; // not a number } while (*number); } } return even; } inline bool isEven(const std::string& number) { return isEven(number.c_str()); } /// for true integers: bool isEven(unsigned long number) { std::string buff; std::ostringstream is(buff); (is << number).flush(); return isEven(buff); } using std::cout; using std::cin; void TestEven() { std::string line; while (cout << "Integer: ",std::getline(cin,line)) cout << isEven(line) << '\n'; }
![]() |
Similar Threads
- The sum of digits of a number (C)
- Sum of digits (C)
- Input an Array then Find sum of digits and num of digits (C++)
- Help with Linked Lists with digits (C++)
- Sum of digits (C++)
Other Threads in the C++ Forum
- Previous Thread: HEX Addition
- Next Thread: Help needed for an iterator going out of bounds
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






