Simple project
I have to design and implement an application that determines and prints the number of odd, even, and zero digits in an interger value.
ex Console.Out.ReadLine("Enter any interger.");
int aValue = int.Parse(Console.In.ReadLine));
Say they enter 1230
Then the output should read something like this
Odd = 2
Even = 1
Zero = 1
The problem is that I don't know how to capture the individual parts of the number and need some help. Someone please point me in the right direction.
BradPosted Nov 14, 2006, 12:33 PM
1. Mathematical approach:
int evens = 0;
int odds = 0;
int zeros = 0;
if(inputNumber == 0){
zeros++;
return;
}
int decimalPlace = 1;
while(inputNumber%decimalPlace != inputNumber){
if(input%decimalPlace*10 == 0){
zeros++
}else if(inputNumber%2*decimalPlace == 0){
evens++;
}else{
odds++;
}
decimalPlace *= 10;
inputNumber = inputNumber - inputNumber%decimalPlace;//makes while conditional too heavy handed but it is more intuitive that way
}
2. String approach:
//after casting inputValue as a string
//since i dont remember the exact functions this is will just be rough pseudo code
for(int i=0; i < inputValue.Length; i++){
int curDecimal = Char.toInt(inputValue.charAt(i));
if(curDecimal ==0){
zeros++;
}else if(curDecimal%2 == 0){
evens++;
}else{
odds++;
}
}
Im sure there are other ways (maybe by doing bit checking?) but these are the ones that come to mind first