The following program segment checks whether a number is an Abundant number or not. A number is said to be Abundant when the sum of its factors (excluding the number itself) is greater than the number. Example: factors of 12 are 1, 2, 3, 4, 6 and their sum is 16. Fill in the blanks with appropriate Java statements: void abundant(int n) { int s = 0; for( (i) __________ ; (ii) __________ ; i++) { if( (iii) __________ ) s = s + i; } if( (iv) __________ ) System.out.println("Abundant Number"); else System.out.println("Not Abundant Number"); }
Fill in blank (i): for( __________ ; ... ; i++)
Topic: Initialising a factor loop
Try it
Pick an option and check your answer. The full worked answer is below either way.
Answer
(a) int i = 1.
Factors are counted from 1, since 1 divides every number. Starting at 0 would be fatal, because n % 0 causes an ArithmeticException — division by zero — and the program would crash. Starting at 2 would wrongly omit the factor 1. Option (d) fails on capitalisation: the primitive type is int, never Int.
int i = 1.