0

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();
}

}
cooldudsk
  • 65
  • 1
  • 4
  • 13

2 Answers2

1
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();
    }
}
DanSchneiderNA
  • 378
  • 4
  • 16
  • that works, but how would you get it to print the divisors of all of the numbers in between the perfect numbers, even if they aren't perfect? – cooldudsk Oct 17 '14 at 03:03
  • In that case make an 'else' for your if (sumOfDivisors == number) statement. In the else just say something like **System.out.printf("Non-Perfect Number: %,d%n[", number); for (Integer i : col) { System.out.printf("%,d, ", i); } System.out.printf("\b\b]%n");** – DanSchneiderNA Oct 17 '14 at 03:10
  • They should really add code formatting for comments. >. – DanSchneiderNA Oct 17 '14 at 03:12
  • thanks, that works great, but do you know how to do it without an array? also it prints some weird double box icon on the end of the list of divisors, where the original number would be. – cooldudsk Oct 17 '14 at 03:34
  • It is possible without an array but it is sort of more confusing. You would place the printf("Number") statement directly below the first for loop. Then place a printf() statement in your if statement in the second for loop to print each divisor. – DanSchneiderNA Oct 17 '14 at 03:39
  • As for the brackets. It is displaying just a [1] for prime numbers. For prime numbers only 1 and itself go into it. The way your code is set up prevents it from showing the number itself so it only displays [1]. To make code more readable, you can put 2 %n signs to double space at the second printf(). In other words, **printf("\b\b]%n%n");** – DanSchneiderNA Oct 17 '14 at 03:42
0
public static void main(String[] args) 

{

    int i=6;
    int n=i;
    int r=0;
    while(i>0){
    i--;

if(i>0 && n%i==0){
    if (i!=0)
        r=r+i;
}

    }   
    System.out.println(r);
    if (r==n)
        System.out.println("its True");
    else
        System.out.println("its False");
}
Rafique Mohammed
  • 3,666
  • 2
  • 38
  • 43