Help?

closed account (2UX23TCk)
How can I input a number for example 54503 and let the program go through all the numbers and check if there is an even number or not? I think it should be inside a loop, and the condition for if it's even should be %2 == 0, but I don't know what the condition of the loop should be?

Sorry for bothering!
Last edited on
First thing I would do is make sure that you take the input as a string. Then you would loop through the string based on it's length so:for(unsigned i = 0; i < Input.length(); ++i)//... and use "atoi()" or something similar with the element access operator to turn each character into an integer. Then you can do your modulous test on that for each element.
closed account (2UX23TCk)
Is there any other way? Because the question tells me to input as an integer not a string.
Sorry for bothering again.
Yes, use this loop.
1
2
3
4
5
6
int n;
int m = n;
while(n > 0){
if( (n%10)%2 == 0) cout << "even digit!";
n/=10;
}

n%10 is the digit. It checks if digit is even, if it is even it outputs "even digit!". The loop goes through all the digits. Notice the n/=10; m is the unmodified original number. Please ask if you have more doubts.
Last edited on
Topic archived. No new replies allowed.