Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:36:47+00:00 2026-06-15T16:36:47+00:00

I am trying to accomplish a task. To print 4 perfect numbers which are

  • 0

I am trying to accomplish a task. To print 4 perfect numbers which are between 1 and 10000.

In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.

Here’s my code:

public class PerfectNumbers
{
    public static void main(String[] args)
    {
        // Perfect numbers!

        for (int number = 1; number < 10000; number++)
        {
            int sum = 0;
            int i = 1;
            while (i < number)
            {

                if (number % i == 0)
                {
                    sum += i;
                    i++;

                }
                else
                {   
                    i++;
                    continue;
                }

                if (sum == number)
                {
                    System.out.println(number);
                }
                else
                {
                    continue;
                }

            }
        }
    }
}

The output is:

6
24 <--- This one is wrong because next must be 28. 
28
496
2016
8128
8190

What is wrong in my code? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:36:48+00:00Added an answer on June 15, 2026 at 4:36 pm

    The if (sum == number) check needs to be done outside the loop. Otherwise you might pick up numbers such that the sum of a subset of divisors equals the number.

    In fact, 24 is one such example since 1+2+3+4+6+8=24. Your code prematurely concludes that 24 is perfect, despite it also being divisible by 12.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The task I was trying to accomplish was that given an input pattern, e.g.
I am trying to accomplish a very simple task of loading a WebView that
I'm trying to accomplish a task and turns out that the code I need
The task I'm trying to accomplish: I have a Rails 3 application that processes
I am trying to accomplish the task of Making a Web request ->getting result
I'm trying to accomplish a task, but having some difficulty.Can someone set me straight
I'm trying to accomplish one simple task (but is not simple for me). I
I'm trying to accomplish a simple task of creating a database with 2 tables
I'm trying to accomplish what should be a very simple task using the ListView
I believe what I am trying to accomplish should be a fairly common task,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.