Java for loop find perfect number

Introduction

A positive integer is a perfect number if it is equal to the sum of all of its positive divisors, excluding itself.

For example, 6 is the first perfect number because 6 = 3 + 2 + 1.

28 is another first perfect number because 28 = 14 + 7 + 4 + 2 + 1.

There are four perfect numbers less than 10,000.

Write a program to find all these four numbers.



public class Main{

    public static void main(String[] strings) {

        for (int number = 6; number <= 10000; number++) {
            int total = 0;
            int divisor = number - 1;

            while (divisor > 0) {

                if (number % divisor == 0) {
                    total += divisor;/*www  .  j  av  a 2 s .c  o  m*/
                }
                divisor--;
            }

            if (number == total) {
                System.out.println(number);
            }

        }
    }
}



PreviousNext

Related