944,008 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3313
  • C++ RSS
Jan 25th, 2008
0

making counter for odd numbers

Expand Post »
im trying to make a counter to count the number of odd numbers in an integer entered by a user but somehow my loop doesnt seem to go on as i hoped it would...im trying practise using the while loop that some of u guys demonstrated on my other thread...

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int oddNum, num, oddCount = 0;
  7. cout << "enter an integer: " << endl;
  8. cin >> num;
  9.  
  10.  
  11. while (oddNum != 0 && num > 10)
  12. {
  13. num = num % 10;
  14. oddNum = num % 2;
  15. oddCount++;
  16. }
  17.  
  18. cout << "number of odds is " << oddCount << endl;
  19. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
Jan 25th, 2008
0

Re: making counter for odd numbers

let me guess. your while does not complete even one loop right? that is because your logic for the loop is all wrong. in addition to that it is needlesly complex.

C++ Syntax (Toggle Plain Text)
  1. int counter = 0;
  2.  
  3. while ( counter <= numberuserentered )
  4. {
  5. counter++;

that should get your loop going from zero until the number that the user entered. now all you need to do is check counter every time to see if it is an odd number. if it is then you add one to some accumulator.

then you close the loop, diplay and exit. check logic first. programming is all about logic.
Last edited by Ravenous Wolf; Jan 25th, 2008 at 6:58 am.
Reputation Points: 33
Solved Threads: 1
Junior Poster
Ravenous Wolf is offline Offline
164 posts
since Aug 2007
Jan 25th, 2008
1

Re: making counter for odd numbers

The definition of the problem is not clear (at least to me).

Given the decimal integer, 523456

Is the answer
1) 3
Becuase there are 3 odd digits (5,3, & 5)

2) 2
Becuase there are 2 unique odd digits (5 & 3)

3) 7
Becuase there are 7 unique odd numbers (5, 3, 523, 52345, 23, 2345 & 345)

4) 261728
Becasue there are 261728 odd numbers before 523456. (1,3,5,7,9,11,13...523455)

Or something else?
Reputation Points: 36
Solved Threads: 0
Light Poster
Passmark is offline Offline
32 posts
since Jan 2008
Jan 25th, 2008
0

Re: making counter for odd numbers

u can make it something like that
when the no is not divisible by 2 then make an increment in the counter..
n kept on checking eveyno..
Reputation Points: 10
Solved Threads: 0
Light Poster
sweety0 is offline Offline
47 posts
since Nov 2006
Jan 25th, 2008
0

Re: making counter for odd numbers

im trying to find the number of odd digits in a number. eg if i enter 12345, the output should be something like 3 odd numbers. but somehow.....theres something not right about my loop i cant figure out...

and i dont understand the loop from Ravenous Wolf....coz if lets say i enter the number 893747457......then the loop would have to count all the way to 893747457??

and sweety0 im attempting to follow that method in my code.....as in when integer % 2 != 0
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
Jan 25th, 2008
-1

Re: making counter for odd numbers

Well if you want to know about digits I usually convert the int into a string. Then you can access each letter in the array. You can use a foreach() loop.

C++ Syntax (Toggle Plain Text)
  1. int counter = 0;
  2. string tempstring = ""+ numberentered;
  3. foreach(char x in tempstring){
  4. if((x=='1')||(x=='3')||(x=='5')||(x=='7')||(x=='9'))){
  5. counter++;
  6. }
  7. }

or in a while loop

C++ Syntax (Toggle Plain Text)
  1. int counter = 0;
  2. string tempstring = ""+ numberentered;
  3. int i =0;
  4. char x = 'a';
  5. while(i < tempstring.Length){
  6. x = tempstring[i];
  7. if((x=='1')||(x=='3')||(x=='5')||(x=='7')||(x=='9'))){
  8. counter++;
  9. }
  10. }

There is probably a faster way to get rid of the multiple or's in the if statement. I would go with the foreach loop. Which just does the while stuff for you really.

Reputation Points: 8
Solved Threads: 0
Newbie Poster
jonesc5 is offline Offline
18 posts
since Oct 2007
Jan 25th, 2008
0

Re: making counter for odd numbers

I think you are close to your goal:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int oddNum, num, oddCount = 0;
  7. cout << "enter an integer: " << endl;
  8. cin >> num;
  9.  
  10.  
  11. while (oddNum != 0 && num > 10)
  12. {
  13. num = num % 10;
  14. oddNum = num % 2;
  15. oddCount++;
  16. }
  17.  
  18. cout << "number of odds is " << oddCount << endl;
  19. }

You don't want this as your control loop though:
C++ Syntax (Toggle Plain Text)
  1. while (oddNum != 0 && num > 10)

This bails you out of the loop too soon (whenever you hit your first even number). Instead use this:

C++ Syntax (Toggle Plain Text)
  1. while (num > 0) // what about one digit numbers?

Also you want to change to divide num by 10 each time, not take a remainder. You'll need an "if" statement in the loop to check oddNum.

C++ does not have foreach. It has for_each in one of its libraries, but I don't think you want to use it here.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Jan 25th, 2008
0

Re: making counter for odd numbers

#include <iostream>
using namespace std;

int main()
{
    int oddNum, num, oddCount = 0;
    cout << "enter an integer: " << endl;
    cin >> num;
    
    
    while (oddNum != 0 && num > 10)
    {
          num = num % 10;
          oddNum = num % 2;
          oddCount++;
    }
    
    cout << "number of odds is " << oddCount << endl;
}

1) You are using oddNum variable without first initialize it.
2) Normally, c = a % b , c must smaller than b . So, num = num % 10 , num will be alway smaller than 10, which make your loop condition false. I think you should change it to num = num / 10 3) oddNum = num % 2; , whenever num is an even number, the loop condition will be become false. Your goal is to run through every digit of the number and count the odd number. However, whenever you encounter the even number, your loop will stop.
4) oddCount++;[ , Without checking whether that digit is an odd number or even number, you keep counting every digit.

My solution would be:
cpp Syntax (Toggle Plain Text)
  1. while(num>0) {
  2. // Whenever the numb is an odd number, it will return 1.
  3. // In C++, all the non-zero numbers mean true and
  4. // zero means false. I put this condition to check the
  5. // current digit whether it is an odd number or even number
  6. if (num%2) oddCount++;
  7.  
  8. // Move to the next digit.
  9. num = num / 10;
  10. }

You also able to do it without using condition inside the loop too:
cpp Syntax (Toggle Plain Text)
  1. while(num>0) {
  2. oddCount += num % 2;
  3. num = num / 10;
  4. }
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Jan 26th, 2008
0

Re: making counter for odd numbers

Invisal, thats a really elegant solution. Puts my clunky string method to shame.
Reputation Points: 8
Solved Threads: 0
Newbie Poster
jonesc5 is offline Offline
18 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help on returning string private variables
Next Thread in C++ Forum Timeline: rename() and save() problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC