Search code examples
javaadditionfactors

Add the factors of a number


public static void main(String[] args) {
  Scanner x = new Scanner(System.in);
  int n = 0;
  int g = 0;
  int term = 0;
  int temp = 0;
  int sum = 0;
  int factor = 1;
  System.out.print("Input N:");

  n = x.nextInt();
  g = n;
  if (n <= 0) {
    System.out.println("Please enter a positive integer");
    System.exit(0);
  }

  if (n > 0) {
    System.out.print("The factors are:");

    while (factor < n) {
      if (n % factor == 0) {
        System.out.print(factor + ",");
      }
      factor++;
    }
  }
}

If I input number 8, the factors are 1,2, and 4. What I am trying to achieve is to add the factors of 8 which are 1,2 and 4, which would result in 7.


Solution

  • import java.util.Scanner;
    
    public class Demo {
    public static void main(String[] args)
    {
        Scanner x=new Scanner(System.in);
        int n=0;int g=0; int term=0;int temp=0;
        int sum=0; int factor=1;
        System.out.print("Input N:");
    
        n=x.nextInt();
        g=n;
        int number = 0;
        if (n<=0)
        {
            System.out.println("Please enter a positive integer");
            System.exit(0);
        }
    
        if (n>0)
        {
            System.out.print("The factors are:");
    
            while (factor<n)
            {
                if (n%factor==0)
                {
                    System.out.println(factor+",");
                    number+=factor;
                }
                factor++;
            }
        }
        System.out.println("Sum = "+number);
    }
    }