Reputation: 11
I have problem with my Java coding to find the perfect number using boolean method. I want to print out like this: Example : ‘6 is a perfect number. 6 is the sum of 1, 2, 3’ Otherwise : ‘9 is not a perfect number’
But I don't know how to make the coding for "6 is the sum of 1, 2, 3". Can anyone help me? Here is my coding :
import java.util.Scanner;
public class trial
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner perfect = new Scanner(System.in);
System.out.print("Enter any integer number : ");
int n = perfect.nextInt();
if(isPerfectNumber(n))
{
System.out.println(n+" is a perfect number");
}
else
{
System.out.println(n+" is not a perfect number");
}
}
public static boolean isPerfectNumber(int n)
{
int sum = 0;
for (int i=1; i<n; i++)
{
if (n%i == 0)
{
sum = sum + i;
}
}
if (sum == n)
{
return true;
}
else
{
return false;
}
}
}
Upvotes: 0
Views: 3499
Reputation: 311448
I'd change isPerfectNumber
to return a List
of the factors that make n
or null
if it's not a perfect number. Then you have a single result that both contains factors and can be used to determine if n
is perfect:
public static void main(String[] args) {
Scanner perfect = new Scanner(System.in);
System.out.print("Enter any integer number : ");
int n = perfect.nextInt();
List<Integer> factors = getPerfectFactors(n);
if (factors != null) {
System.out.println
(n + " is a perfect number. It's the sum of" +
factors.stream()
.map(String::valueOf)
.collect(Collectors.joining(", "));
} else {
System.out.println(n+" is not a perfect number");
}
}
public static List<Ingeger> getPerfectFactors(int n) {
int sum = 0;
List<Ingeger> factors = new LinkedList<>();
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum += i;
factors.add(i);
}
if (sum > n) { // Early return optimization, not material to the solution
return null;
}
}
if (sum == n) {
return factors;
} else {
return null;
}
}
Upvotes: 1
Reputation: 4598
Well in the for loop if condition if modulus is true then you know the i
has to be kept in track somehow.
So I would declare an arraylist of integers to keep track of summation numbers.
ArrayList<Integer> numbs = new ArrayList<Integer>();
Then
public static boolean isPerfectNumber(int n)
{
int sum = 0;
for (int i=1; i<n; i++)
{
if (n%i == 0)
{
numbs.add(i); //here keep track of those summation numbers
sum = sum + i;
}
}
if (sum == n)
{
return true;
}
else
{
return false;
}
}
Now while printing you can do like
System.out.println(n + " is a perfect number. " + n + " is sum of " + StringUtils.join(numbs, ","));
Don't forget to import StringUtils: import org.apache.commons.lang3.StringUtils commons-lang3
library.
Upvotes: 0
Reputation: 881573
Well, you already know how to get the factors of the number, as per the if (n%i == 0)
line.
So, hint only since this is almost certainly class work.
At the same point you add the factor to the sum, you should add it to a list of some description and have that made available to the calling function.
One possibility would be to return a list, the second and subsequent elements being all the factors of the given number, and the first element being the sum of those.
So, for 6
, you would get the list {6, 1, 2, 3}
, 12
would give you {16, 1, 2, 3, 4, 6}
, and 7
would give you {7, 1}
.
That way, you simply have to check the original number against the first element and, if they're equal, print out the other elements. In other words, pseudo-code such as:
input num
factorList = getFactorList(num)
if factorList[0] == num:
print num, " is perfect, factors are:"
for idx = 1 to factorList.size() - 1 inclusive:
print " ", factorList[idx]
println "."
else:
println num, " is not perfect."
Upvotes: 1