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 (iii): int rem = __________
Topic: Extracting the last digit
Try it
Pick an option and check your answer. The full worked answer is below either way.
Answer
(c) n % 10;.
The modulus operator % gives the remainder, and dividing by 10 leaves exactly the last digit: 674 % 10 = 4. That is the pair to memorise — % 10 TAKES the last digit while / 10 REMOVES it. Option (b) would give 67, the number with its last digit removed, which is what the NEXT blank needs, not this one.
n % 10;.