java - Boolean expression (x / 2) * 2 == x to test -
this 1 of lab questions.
you can test see if integer, x, or odd using boolean expression (x / 2) * 2 == x. integers make expression true, , odd integers make expression false. use loop iterate 5 times. each iteration, request integer user. print each integer user types, , whether or odd. keep number of , odd integers user types, , print “done” when finished, user won’t try type integer. finally, print out number of , odd integers entered.
i know question requires me do. however, don’t quite understand why boolean expression, (x/2)*2 == x, can test whether integer ever number or odd number. lets number 59, odd integer obviously. 59 divided 2 29.5. 29.5 times 2, equals 59. no matter x is, (x/2)*2 equals x. how make expression false when integer odd. can determine should print.
because you're dealing integers, there's rounding down nearest round number.
59/2 = 29
when elements integers.
multiplying result 2 gives 58
, since 2 numbers aren't same, deduce number, 59, odd.
Comments
Post a Comment