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 (v): for( __________ ; x <= ... ; x += 0.5)
Topic: Initialising a double loop variable
Try it
Pick an option and check your answer. The full worked answer is below either way.
Answer
(a) double x = 0.5.
Two decisions are being tested. The series starts at 0.5, not 0.0, so options (c) and (d) are out. And the type must be the PRIMITIVE double in lower case — Double with a capital D is the wrapper class, an object, which is not what a loop counter should be. A double loop variable is perfectly legal, and it is required here because the step is 0.5; an int counter could not express that.
double x = 0.5.