Given below is a class with the following specifications: class name : Number void Display(int n) - To extract and print each digit of the given number, from the last digit to the first digit, on separate lines. Example: n = 674 Output: 4 / 7 / 6 void Display() - To print numbers from 0.5 to 5.0 with an update of 0.5. Fill in the blanks of the given program with appropriate statements: class (i) __________ { void Display(int n) { while( (ii) __________ ) { int rem = (iii) __________ System.out.println(rem); n = (iv) __________ } } void Display() { for( (v) __________ ; x <= (vi) __________ ; x += 0.5) System.out.println(x); } }
Fill in blank (iv): n = __________
Topic: Removing the last digit
Try it
Pick an option and check your answer. The full worked answer is below either way.
Answer
(b) n / 10;.
Integer division by 10 drops the last digit and shifts the rest right: 674 / 10 = 67 (the fractional .4 is discarded because both operands are ints). Repeating this reduces the number to 0 and ends the loop. Using % 10 here instead would leave n stuck at the same digit for ever — an infinite loop. The pairing of % 10 to take and / 10 to remove is the whole engine of every digit question.
n / 10;.