Help?

May 13, 2016 at 5:04pm
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 May 13, 2016 at 5:09pm
May 13, 2016 at 5:21pm
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.
May 13, 2016 at 6:25pm
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.
May 14, 2016 at 4:36am
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 May 14, 2016 at 4:38am
Topic archived. No new replies allowed.