Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java Sum of all the factors of a number

// Simple Java program to
// find sum of all divisors
// of a natural number
import java.io.*;
 
class GFG {
 
    // Function to calculate sum of all
    //divisors of a given number
    static int divSum(int n)
    {
         if(n == 1)
           return 1;
        // Final result of summation
        // of divisors
        int result = 0;
     
        // find all divisors which divides 'num'
        for (int i = 2; i <= Math.sqrt(n); i++)
        {
            // if 'i' is divisor of 'n'
            if (n % i == 0)
            {
                // if both divisors are same
                // then add it once else add
                // both
                if (i == (n / i))
                    result += i;
                else
                    result += (i + n / i);
            }
        }
     
        // Add 1 and n to result as above loop
        // considers proper divisors greater
        // than 1.
        return (result + n + 1);
         
    }
     
    // Driver program to run the case
    public static void main(String[] args)
    {
        int n = 30;
        System.out.println(divSum(n));
    }
}
 
// This code is contributed by Prerna Saini.
Comment

PREVIOUS NEXT
Code Example
Java :: java bild skalieren bufferedimage 
Java :: two array structures in java 
Java :: Write a java program to merge three singly linked list elements 
Java :: We would like to make a member of a class can access in all subclasses regardless of what package the subclass is in. Which one of the following keywords would achieve this? 
Java :: how to create a udp protocol for transfer a big quantity of files java 
Java :: TestNG Data Provider 
Java :: is type java 
Java :: program to calculate and return the sum of distance between the adjacent numbers in an array of positive integer java 
Java :: Caused by: java.lang.IllegalStateException: stream has already been operated upon or closed 
Java :: how to implement count steps in android 
Java :: for each loop summation 
Java :: java print color in console eclipse 
Java :: regex pattern for car plates 
Java :: converting temperature from fahrenheit to celsius 
Java :: codegrepper java instanceof 
Java :: java switch case enum 
Java :: reference value in array list java syntax 
Java :: integer to roman 
Java :: ein wort in buchstaben zerlegen java 
Java :: java gson get object without class 
Java :: value of for loop i in Jbutton 
Java :: spigot scoreboard objective 
Java :: simple text formatter as in textbook 
Java :: initialize generic array java 
Java :: Java 17 not showing in linux 
Java :: android how to get position of a row in listview 
Java :: treeset order in java 
Java :: android java update image dynamically 
Java :: load local json 
Java :: model mapper with Page 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =