For example I ask the user to enter a 4 digits integer number. For example the user enter 4875. I want to display odd digits (7 5) and even digits (4 8). By Extract each digit of the number using integer division and store the results in separate variables.
for example, that 4875 is 4*1000+8*100+7*10+5

After I extract each digit of the number by following:
int firstDigit = x / 1000;
int secondDigit = (x - (firstDigit *1000)) / 100;
int thirdDigit = (x - (firstDigit *1000) - (secondDigit *100)) /10;
int fourthDigit = (x - (firstDigit *1000) - (secondDigit *100) - (thirdDigit * 10));

and then I need to use "If" statement to display odd digits and even digits. Can anyone show me how?

8 Years
Discussion Span
Last Post by Grn Xtrm

Test if the number is even or odd using the modulus operator.
If the number % 2 equals 0, then the number is even.
If the number % 2 equals 1, then the number is odd.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.