| | |
Even sum of digits
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2009
Posts: 29
Reputation:
Solved Threads: 0
I need to write a program that will count how many numbers within an interval have an even sum of digits. you are given two numbers, a and b. i wrote a little program that is a little slow.. it checks for the sum of the digits for every number in the interval. Any suggestions about a better algorithm appreciated.
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string c; stringstream ss; int a,b, rezx, count; cin>>a>>b; for(int i=a; i<b; i++) { ss<<i; c=ss.str(); ss.str(""); for(int x=0; x<c.length(); x++) { rezx=rezx+c[x]; } if(rezx%2==0) { count++; } rezx=0; } cout<<count-1; system("pause"); return 0; }
I think the following algorithm is a better one:
If you know the starting number of the interval:
-> Check whether the sum of it's digits are even (you could use the modulo operator for this)
-> If the sum was even, you automatically know the next number's sum of digits is odd
e.g: 450 => odd ; 451 => even ; 452 => odd ; 453 => even ; etc. ...
Edit:: So actually you don't have to check each number's sum of digits
Hope this helps !
If you know the starting number of the interval:
-> Check whether the sum of it's digits are even (you could use the modulo operator for this)
-> If the sum was even, you automatically know the next number's sum of digits is odd
e.g: 450 => odd ; 451 => even ; 452 => odd ; 453 => even ; etc. ...
Edit:: So actually you don't have to check each number's sum of digits
Hope this helps !
Last edited by tux4life; Apr 10th, 2009 at 6:59 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
The method described here is probably a more efficient one as yours as it isn't converting the number to a string each time ...
Edit:: I have to admit my previous post was a bad one ...
Edit:: I have to admit my previous post was a bad one ...
Last edited by tux4life; Apr 10th, 2009 at 7:56 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
It's not only the most slow algorithm (the original one) but it's a wrong algorithm. You have got not a sum of digits but a sum of char codes of digits. It's the other story that char code of '0' is even numbert in ASCII table; the C language does not specify this fact.
It's so easy to get the last digit of integer n:
It's so easy to get the last digit of integer n:
n%10 then get the last digit of n /= 10 and so on... •
•
•
•
should i try to make an algorithm that checks if the first number's sum of digits is even and than to count the interval until 9 is reached and than to switch? example:
289-false
290-false
291-true
292-false
293-true
....
299-true
300-false
and than to count the true ones
"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 don't understand - why do you have the numbers input as 'int'? Why don't you have the numbers input directly into a string and then use the operator [] to move through the digits and count values? I think it could be much more efficient - just run a loop like
for(index=0;index<number.length();index++) and add every digit to a variable that holds the sum. If you assume ASCII, you won't even need to convert the character digits into digits, because their ASCII values are even for even digits. Last edited by unbeatable0; Apr 10th, 2009 at 10:01 am. Reason: Typo
•
•
•
•
I don't understand - why do you have the numbers input as 'int'? Why don't you have the numbers input directly into a string and then use the operator [] to move through the digits and count values? I think it could be much more efficient - just run a loop like for(index=0;index<number.length();index++) and add every digit to a variable that holds the sum. If you assume ASCII, you won't even need to convert the character digits into digits, because their ASCII values are even for even digits. Why making it difficult if it can be easy ?
Look at ArkM's post !
Last edited by tux4life; Apr 10th, 2009 at 10:11 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
![]() |
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 based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






