cooldudsk cooldudsk - 13 days ago 7
Java Question

Java perfect numbers nested for loop

I made this with the help of a friend, in order to find the perfect numbers(numbers where sum of divisors = original number) sum of up to the number that the user enters. Can someone help me convert it so that it prints each number on a line, even if it isn't a perfect number, and lists all of its divisors, then if it is perfect it prints perfect number after the divisors? so like "6: 1, 2, 3, 6, perfect number"without using arrays?

import java.util.*;
public class PerfNumbers {
public static void main(String[] args){
System.out.println("Enter A Number:");
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
int sumOfDivisors = 0;

for (int number = 1; number <= input; number++){
for (int divisor = 1; divisor < number; divisor++)
{

if (number % divisor == 0)

sumOfDivisors += divisor;
}

if (sumOfDivisors == number)
{

System.out.println(number);
}

sumOfDivisors = 0;
}

sc.close();
}

}

Answer
import java.util.*;

/**
 *
 * @author Tyler Weaver
 */
public class Test {

    public static void main(String[] args) {
        System.out.print("Enter A Number: ");
        Scanner sc = new Scanner(System.in);
        int input = sc.nextInt();
        int sumOfDivisors = 0;
        Collection<Integer> col = new ArrayList<>();

        for (int number = 1; number <= input; number++) {
            for (int divisor = 1; divisor < number; divisor++) {

                if (number % divisor == 0) {
                    sumOfDivisors += divisor;
                    col.add(divisor);
                }
            }

            if (sumOfDivisors == number) {

                System.out.printf("Perfect Number: %,d%n[", number);
                for (Integer i : col) {
                    System.out.printf("%,d, ", i);
                }
                System.out.printf("\b\b]%n");
            }

            sumOfDivisors = 0;
            col.clear();
        }

        sc.close();
    }

}

Just added a collection to keep track of the divisors, then added a printf() statement to properly format.

EDIT: Code without using a collection.

import java.util.*;

/**
 *
 * @author Tyler Weaver
 */
public class Test {

    public static void main(String[] args) {
        System.out.print("Enter A Number: ");
        Scanner sc = new Scanner(System.in);
        int input = sc.nextInt();
        int sumOfDivisors = 0;

        for (int number = 2; number <= input; number++) {
            System.out.printf("Number: %,d%n[", number);

            for (int divisor = 1; divisor < number; divisor++) {

                if (number % divisor == 0) {
                    sumOfDivisors += divisor;
                    System.out.printf("%,d, ", divisor);
                }
            }

            System.out.printf("\b\b]%n");

            if (sumOfDivisors == number) {
                System.out.printf("Therefore %,d is a perfect number.%n%n",
                        number);
            } else {
                System.out.printf("%n");
            }

            sumOfDivisors = 0;
        }

        sc.close();
    }
}