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 (ii): for( ... ; __________ ; i++)
Topic: Loop limit for proper factors
Try it
Pick an option and check your answer. The full worked answer is below either way.
Answer
(b) i < n.
The definition says the sum EXCLUDES the number itself, so the loop must stop before reaching n — hence i < n and not i <= n. This is the whole point of the question. With i <= n, the number itself would be added to the total, and since n + (other factors) always exceeds n, every number would be reported as abundant. For 12 the correct sum is 1 + 2 + 3 + 4 + 6 = 16, not 28.
i < n.