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

# 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. ## 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.

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.");
}
}
}
}  Reply With Quote

2. ## 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.

[code]
[/code]

to get highlighting and preserve formatting.  Reply With Quote

3. ## 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.");
}
}
}
}```  Reply With Quote

4. ## 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?  Reply With Quote

5. ## 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  Reply With Quote

6. ## 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  Reply With Quote

7. ## 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.  Reply With Quote

8. ## 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.  Reply With Quote

9. ## 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  Reply With Quote

10. ## 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.  Reply With Quote

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

winwin (February 16th, 2021) 