I'm trying to print out if a number is perfect or not by having the user enter in a number. When I enter a perfect number like 6, for example, it tells me that it is not a perfect number and can't figure out why. My final code needs to print out like 6 = 1 + 2 + 3. But I'm not that far yet.
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a positive integer: ");
int n = scanner.nextInt();
int sum = 0;
for(int i = 1;i<n;i++){
if(n%2==0)
sum += i;
}
if(sum==n){
System.out.println(n + " is a positive number");
}
else {
System.out.println(n + " is not a positive number");
}
if(n%2==0)
should be
if(n%i==0)
Otherwise your sum would be the sum of all numbers from 1
to n-1
for even n
, or 0
for odd n
.