2. Proper Divisors A proper divisor is an integer that evenly divides a source i
ID: 3870497 • Letter: 2
Question
2. Proper Divisors A proper divisor is an integer that evenly divides a source integer (i.e., the remainder is 0) and is strictly less than the source integer. For example, the proper divisors of 20 are 1, 2, 4, 5, and 10 (20 is also a divisor, but not a proper one). Their sum is 1 +2+4+5+ 10, or 22. Complete the addDivisors ) method, which accepts a single int as its argument. This method uses a loop to compute the sum of the input integer's proper divisors, and then returns that sum as an integer value. You may assume that the input to this method is always a positive integer value.Explanation / Answer
import java.util.*;
class Test
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer to process : ");
int val = sc.nextInt();
System.out.println(" Sum of proper divisors : "+addDivisors(val)); //call to function
}
public static int addDivisors(int val)
{
int sum = 0; //initialize sum to 0
for(int i=1; i<val;i++) // check each element from 1 to number less than val
{
if(val%i == 0) // if val is divisible by i
sum = sum + i; // if divisible add the value of i to sum
}
return sum;
}
}
Output:
Enter an integer to process : 20
Sum of proper divisors : 22
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.