making counter for odd numbers

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

Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

making counter for odd numbers

 
0
  #1
Jan 25th, 2008
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...

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 164
Reputation: Ravenous Wolf is an unknown quantity at this point 
Solved Threads: 1
Ravenous Wolf Ravenous Wolf is offline Offline
Junior Poster

Re: making counter for odd numbers

 
0
  #2
Jan 25th, 2008
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.

  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.
... what society overwhelmingly asks for is snake oil. Of course, the snake oil has the most impressive names —otherwise you would be selling nothing— like "Structured Analysis and Design", "Software Engineering", "Maturity Models", "IPSE", "MIS", "OO", "BPRE".... by Edsger W. Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 31
Reputation: Passmark is an unknown quantity at this point 
Solved Threads: 0
Passmark's Avatar
Passmark Passmark is offline Offline
Light Poster

Re: making counter for odd numbers

 
1
  #3
Jan 25th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 47
Reputation: sweety0 is an unknown quantity at this point 
Solved Threads: 0
sweety0 sweety0 is offline Offline
Light Poster

Re: making counter for odd numbers

 
0
  #4
Jan 25th, 2008
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..
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

Re: making counter for odd numbers

 
0
  #5
Jan 25th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 18
Reputation: jonesc5 is an unknown quantity at this point 
Solved Threads: 0
jonesc5 jonesc5 is offline Offline
Newbie Poster

Re: making counter for odd numbers

 
-1
  #6
Jan 25th, 2008
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.

  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

  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.

Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: making counter for odd numbers

 
0
  #7
Jan 25th, 2008
I think you are close to your goal:

  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:
  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: making counter for odd numbers

 
0
  #8
Jan 25th, 2008
#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:
  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:
  1. while(num>0) {
  2. oddCount += num % 2;
  3. num = num / 10;
  4. }
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 18
Reputation: jonesc5 is an unknown quantity at this point 
Solved Threads: 0
jonesc5 jonesc5 is offline Offline
Newbie Poster

Re: making counter for odd numbers

 
0
  #9
Jan 26th, 2008
Invisal, thats a really elegant solution. Puts my clunky string method to shame.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC