| | |
making counter for odd numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
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)
#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; }
•
•
Join Date: Aug 2007
Posts: 164
Reputation:
Solved Threads: 1
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.
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.
C++ Syntax (Toggle Plain Text)
int counter = 0; while ( counter <= numberuserentered ) { 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
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?
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?
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Oct 2007
Posts: 18
Reputation:
Solved Threads: 0
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.
or in a while loop
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.
C++ Syntax (Toggle Plain Text)
int counter = 0; string tempstring = ""+ numberentered; foreach(char x in tempstring){ if((x=='1')||(x=='3')||(x=='5')||(x=='7')||(x=='9'))){ counter++; } }
or in a while loop
C++ Syntax (Toggle Plain Text)
int counter = 0; string tempstring = ""+ numberentered; int i =0; char x = 'a'; while(i < tempstring.Length){ x = tempstring[i]; if((x=='1')||(x=='3')||(x=='5')||(x=='7')||(x=='9'))){ counter++; } }
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.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
I think you are close to your goal:
You don't want this as your control loop though:
This bails you out of the loop too soon (whenever you hit your first even number). Instead use this:
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.
C++ Syntax (Toggle Plain Text)
#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; }
You don't want this as your control loop though:
C++ Syntax (Toggle Plain Text)
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)
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.
#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)
while(num>0) { // Whenever the numb is an odd number, it will return 1. // In C++, all the non-zero numbers mean true and // zero means false. I put this condition to check the // current digit whether it is an odd number or even number if (num%2) oddCount++; // Move to the next digit. num = num / 10; }
You also able to do it without using condition inside the loop too:
cpp Syntax (Toggle Plain Text)
while(num>0) { oddCount += num % 2; num = num / 10; }
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: help on returning string private variables
- Next Thread: rename() and save() problem
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






