Tag Archives: sum of divisor

Java program to find Sum of proper divisor of a number

Given a natural number n, calculate sum of all its proper divisors. A proper divisor of a natural number is the divisor that is strictly less than the number.

For example, number 10 has 3 proper divisors: 1, 2, 5 and the divisor summation is: 1 + 2 + 5 = 8.



import java.util.*;
import java.lang.*;
import java.io.*;

class SumOfDivisors {
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
            System.out.println("Enter a number:");
            int n=sc.nextInt();
            int result=1;
            for(int i=2;i<Math.sqrt(n);i++){
                if(n%i==0){
                    if(n/i==0){
                        result +=i;
                    }else{
                        result +=(i+n/i);
                    }
                }
            }
            System.out.println(result);
        
    }
}