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 (iii): if( __________ )
Topic: Testing for a factor
Try it
Pick an option and check your answer. The full worked answer is below either way.
Answer
(a) n % i == 0.
A number i is a factor of n exactly when it divides n leaving NO remainder, which is n % i == 0. Note carefully that it is n % i, not i % n — the number is divided BY the candidate factor. Option (c) tests only whether n is even, and option (d) uses division instead of remainder, which would be true only for n = 0 or 1.
n % i == 0.