Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Please Help Me on how to make a program which reads a positive non-zero integer as input and checks i it is perfect, deficient or abundant.

  1. #1
    Junior Member
    Join Date
    Feb 2021
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Please Help Me on how to make a program which reads a positive non-zero integer as input and checks i it is perfect, deficient or abundant.

    /*A positive, non-zero integer, N, is said to be perfect if the sum of its positive proper divisors is equal to the number itself. If this sum is less than N, the numbers is said to be deficient. If the sum is greater than N, the number is said to be abundant.*/

    //I tried to make one which is the one below but it didn't work.
    //Please help me, I'm still new to programming, tysm


    import java.util.Scanner;
    public class *{
    public static void main(String[] args){
    Scanner kbd = new Scanner(System.in);

    System.out.print("Input N: ");
    int n = kbd.nextInt();
    int i = 1;
    int sum = 1;

    for(i=1; i<=n; i++){
    if (n % i == 0){
    sum = sum + i;

    if (sum < n)
    System.out.print(n + " is abundant.");
    else if (sum == n)
    System.out.print(n + " is perfect.");
    else if (sum > n)
    System.out.print(n + " is deficient.");
    }
    }
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please Help Me on how to make a program which reads a positive non-zero integer as input and checks i it is perfect, deficient or abundant.

    it didn't work
    Please explain. What does the code do now?
    What are the tests for each of the qualities the number should have:
    positive non-zero integer
    perfect,
    deficient
    abundant.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2021
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please Help Me on how to make a program which reads a positive non-zero integer as input and checks i it is perfect, deficient or abundant.

    I need to make a java program that will read an input positive integer and then checks if it is a perfect, deficient or abundant number using the for-loop.
    for example: 6 is a perfect number since 6=1+2+3; 8 is deficient since 8>1+2+4; 12 is abundant since 12<1+2+3+4+6.

    The output expected should be:
    Input N: 6
    6 is perfect.

    But my code's output is:
    Input N: 6
    6 is abundant.6 is abundant.6 is deficient.6 is deficient.

    I want to know where I got it wrong.

    This is the code I did

    import java.util.Scanner;
    public class Perfect{
       public static void main(String[] args){
          Scanner kbd = new Scanner(System.in);
     
          System.out.print("Input N: ");
            int n = kbd.nextInt();
            int i = 1;
            int sum = 1;
     
            for(i=1; i<=n; i++){
                if (n % i == 0){
                  sum = sum + i;         
     
                   if (sum < n)
                          System.out.print(n + " is abundant.");
                      else if (sum == n)
                          System.out.print(n + " is perfect.");
                         else if (sum > n)
                             System.out.print(n + " is deficient.");           
               } 
            }
       }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please Help Me on how to make a program which reads a positive non-zero integer as input and checks i it is perfect, deficient or abundant.

    Can you define these terms for a number?
    perfect,
    deficient
    abundant.

    Are those 3 terms mutually exclusive: IE a number can only be one of them and not more than one?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2021
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please Help Me on how to make a program which reads a positive non-zero integer as input and checks i it is perfect, deficient or abundant.

    the number can only be one of them

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please Help Me on how to make a program which reads a positive non-zero integer as input and checks i it is perfect, deficient or abundant.

    Can you post definitions for the terms:
    perfect,
    deficient
    abundant.

    They need to be defined before any code can be written to solve for them
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2021
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please Help Me on how to make a program which reads a positive non-zero integer as input and checks i it is perfect, deficient or abundant.

    The integer is perfect if the sum of its positive proper divisors is equal to the number itself.
    If this sum is less than the integer, the numbers is deficient.
    If the sum is greater than the integer, the number is abundant.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please Help Me on how to make a program which reads a positive non-zero integer as input and checks i it is perfect, deficient or abundant.

    proper divisors
    Another term that needs definition.

    Should the sum be computed BEFORE the test is done? In other words, do the test outside of the loop that does the summing.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2021
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please Help Me on how to make a program which reads a positive non-zero integer as input and checks i it is perfect, deficient or abundant.

    Proper divisors of a number is its factors other than itself.
    The program should compute the sum first in order to identify whether the number is perfect, deficient or abundant.

    Can you point out, what's wrong with my code? I don't really know what to do

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please Help Me on how to make a program which reads a positive non-zero integer as input and checks i it is perfect, deficient or abundant.

    Did you move the tests outside of the loop so that they are done AFTER the sum has been computed?

    what to do
    Try debugging the code by adding some print statements inside of the loop that print out the values of sum and i so that you can see what the computer sees when the code is executed. The print outs will help you see what might be wrong with the code.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    winwin (February 16th, 2021)

Similar Threads

  1. calculate the factors of a positive integer number
    By ATB in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2014, 07:48 PM
  2. Replies: 1
    Last Post: August 9th, 2013, 10:26 AM
  3. It is possible that not using array to list the positive integer <100??
    By lim131 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2012, 10:52 AM
  4. Replies: 8
    Last Post: April 14th, 2012, 12:24 PM
  5. Replies: 2
    Last Post: November 30th, 2011, 07:35 PM

Tags for this Thread