Java: How to test if individual characters are even or odd? -
in java, how test if character or odd digit?
here have far:
import java.util.scanner; public class oddoreven{ public static void main(string args[]){ scanner input = new scanner(system.in); int number, digit1, digit2, digit3; system.out.print( "enter three-digit number: " );// prompt number = input.nextint(); // read number // determine 3 digits digit1 = number / 100; digit2 = number % 100 / 10; digit3 = number % 100 % 10; if (digit1 % 2 == 0 && digit2 % 2 == 0 && digit3 % 2 == 0); system.out.println( "this number contains digits."); if (digit1 % 2 != 0 && digit2 % 2 == 0 && digit3 % 2 == 0); system.out.println("this number contains both odd , digits."); if (digit1 % 2 != 0 && digit2 % 2 != 0 && digit3 % 2 == 0 ); system.out.println("this number contains both odd , digits."); if (digit1 % 2 == 0 && digit2 % 2 != 0 && digit3 % 2 == 0 ); system.out.println("this number contains both odd , digits."); if (digit1 % 2 == 0 && digit2 % 2 != 0 && digit3 % 2 != 0); system.out.println("this number contains both odd , digits."); if (digit1 % 2 != 0 && digit2 % 2 != 0 && digit3 % 2 != 0); system.out.println("this number contains odd digits."); } }
if number working int
(or similar primitive type long
) can this
int num = // while (num != 0) { int digit = num % 10; system.out.println(digit + " " + (digit % 2 == 0 ? "even" : "odd")); num /= 10; }
this iterate on digits right left.
Comments
Post a Comment