Computer >> Computer tutorials >  >> Programming >> Java

Java Program to find minimum sum of factors of a number


To find minimum sum of factors of a number, the Java code is as follows −

Example

public class Demo {
   static int minimum_sum(int num){
      int my_sum = 0;
      for (int i = 2; i * i <= num; i++){
         while (num % i == 0){
            my_sum += i;
            num /= i;
         }
      }
      my_sum += num;
      return my_sum;
   }
   public static void main(String[] args){
      int num = 350;
      System.out.println("The minimum sum of factors of the number are ");
      System.out.println(minimum_sum(num));
   }
}

Output

The minimum sum of factors of the number are
19

A class named Demo contains a static function named ‘minimum_sun’, that initializes a sum to 0, and iterates through the number and checks to find the minimum sum from all the factors of that specific number. In the main function, the value for the number is defined, and the function is called by passing the number as the parameter. Relevant message is displayed on the console.