Even sum of digits

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Even sum of digits

 
0
  #21
Apr 10th, 2009
Originally Posted by unbeatable0 View Post
The result is the same.
Both programs were intended to calculate the sum of the digits of a number, however, the second program's output is different, try it !
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: Even sum of digits

 
0
  #22
Apr 10th, 2009
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.
Last edited by unbeatable0; Apr 10th, 2009 at 2:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 677
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Even sum of digits

 
0
  #23
Apr 10th, 2009
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.

  1. string a;
  2. unsigned short int Sum=0,i;
  3. a="12522";
  4. for(i=0;i<a.length();i++)
  5. {
  6. string b;
  7. b=a[i];
  8. Sum+=atoi(b.c_str())];
  9. }
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Even sum of digits

 
0
  #24
Apr 10th, 2009
Originally Posted by Sky Diploma View Post
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.

  1. string a;
  2. unsigned short int Sum=0,i;
  3. a="12522";
  4. for(i=0;i<a.length();i++)
  5. {
  6. string b;
  7. b=a[i];
  8. Sum+=atoi(b.c_str())];
  9. }
Yeah, but with atoi you're again doing conversions, which means it's not the most efficient way ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Even sum of digits

 
0
  #25
Apr 10th, 2009
Originally Posted by unbeatable0 View Post
just because the ASCII value of '0' modulo 2 is 0.
Agreed, but that's logic: 48 modulo 2 is equal to 0 ...

Originally Posted by unbeatable0 View Post
We're interested to know if the number's sum of digits is even or odd
Agreed.

Originally Posted by unbeatable0 View Post
or in other words: if it's 0 (modulo 2) or 1 (modulo 2) respectively.
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."
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 677
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Even sum of digits

 
0
  #26
Apr 10th, 2009
Originally Posted by tux4life View Post
Agreed, but that's logic: 48 modulo 2 is equal to 0 ...


Agreed.


You'll have to explain this carefully using an example ...
  1. 48%2=0;
THats true.
Unbeatable wants
  1. 48,
  2. 4+8=12;//SUM OF DIGITS
  3. 12%2=0// even
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Even sum of digits

 
0
  #27
Apr 10th, 2009
Originally Posted by Sky Diploma View Post
Unbeatable wants
  1. 48,
  2. 4+8=12;//SUM OF DIGITS
  3. 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."
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: Even sum of digits

 
0
  #28
Apr 11th, 2009
I thought I would throw another solution using recursion out there. Not fully tested but should do the job.
  1. int sum_digits(int number)
  2. {
  3. int remainder = number%10;
  4. if (remainder > 0)
  5. return remainder+sum_digits((number-remainder)/10);
  6. else if (number >= 10 && remainder == 0)
  7. return sum_digits(number/10);
  8. return number;
  9. }
  10.  
  11. bool sum_digits_even(int number)
  12. {
  13. return !(sum_digits(number) % 2);
  14. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 29
Reputation: FREEZX is an unknown quantity at this point 
Solved Threads: 0
FREEZX FREEZX is offline Offline
Light Poster

Re: Even sum of digits

 
0
  #29
Apr 12th, 2009
sorry your algorithm is not working correctly. Any ideas
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Even sum of digits

 
1
  #30
Apr 12th, 2009

  1. bool isEven(const char* number)
  2. {
  3. bool even = false;
  4. if (number) {
  5. if (*number == '-' || *number == '+')
  6. number++;
  7. if (*number) {
  8. even = true;
  9. do switch (*number++) {
  10. case '1': case '3': case '5': case '7': case '9':
  11. even = !even;
  12. continue;
  13. case '0': case '2': case '4': case '6': case '8':
  14. continue;
  15. default: return false; // not a number
  16. } while (*number);
  17. }
  18. }
  19. return even;
  20. }
  21. inline
  22. bool isEven(const std::string& number) {
  23. return isEven(number.c_str());
  24. }
  25. /// for true integers:
  26. bool isEven(unsigned long number)
  27. {
  28. std::string buff;
  29. std::ostringstream is(buff);
  30. (is << number).flush();
  31. return isEven(buff);
  32. }
  33. using std::cout;
  34. using std::cin;
  35. void TestEven()
  36. {
  37. std::string line;
  38. while (cout << "Integer: ",std::getline(cin,line))
  39. cout << isEven(line) << '\n';
  40. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC