Results 1 to 10 of 10
Thread: Perfect number assignment
- 11-23-2011, 07:20 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Perfect number assignment
So I have an assignment to create a program with 3 methods that test to see if the number entered is a perfect number. What I have is close to being done but I'm having a problem with printing my factors in a descending order. When I try to decrement my counter, i , it creates an infinite loop. Any suggestions on how to correct that? Also another thing is that when you test numbers 1-9, 9 shows up with 1 & 3 as factors, not sure why. I'm guessing it has to do in the testPerfect method, but where?
Here is my testPerfect and printPerfect methods, if you need to see the main I can post it. The main prints everything except the factors.
Java Code:public static boolean testPerfect(int number2) { int sum = 0; boolean testing = false; for (int i = 1; i < number2/2; i++ ) { if ( number2 % i == 0) sum = sum + i; if (sum == number2/2) testing = true; } return testing; } // end testPerfect public static void printFactors(int print) { for (int i = 1; i <= print /2; i++) { if (print % i == 0) { System.out.print(i + " "); } } }
Last edited by MusicMan; 11-23-2011 at 07:24 PM.
- 11-23-2011, 08:37 PM #2
Re: Perfect number assignment
When I try to decrement my counter, i , it creates an infinite loop.
Do you need to use two variables, one to print out, one to control the loop?
- 11-23-2011, 09:49 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Re: Perfect number assignment
it's in the printFactors method.
Java Code:public static void printFactors(int print) { for (int i = 1; i <= print /2; i++) { if (print % i == 0) { System.out.print(i + " "); } } }
- 11-23-2011, 09:54 PM #4
Re: Perfect number assignment
Can you explain what the problem is with the code you just posted?
- 11-23-2011, 09:56 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Re: Perfect number assignment
Certainly. It is printing out the factors in increasing order. I need them to be in decreasing order.
- 11-23-2011, 09:56 PM #6
Re: Perfect number assignment
Then start at the other end and work down.
- 11-23-2011, 09:59 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Re: Perfect number assignment
I've changed the print statement to:
Java Code:System.out.printf("%d", --i);
- 11-23-2011, 10:06 PM #8
Re: Perfect number assignment
What stops the loop from executing? If you change the value of the loop control variable then you can effect how if and when the loop will stop.
Take a piece of paper and write down the starting value and under it the values you want to have printed out.
Say you start with 14, what will be in the list.
What value will i start with and how will i change as you go through the loop?
What will be the ending value of i?
How do you get from the starting value to the ending value of it?
Answering those questions will tell you how to code the if statement.
Go back and look at the definitions of the 3 clauses in the if statement.
- 11-23-2011, 10:06 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 01-15-2012, 06:42 AM #10
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 10
Similar Threads
-
Perfect number 1-1000 program help
By ImWithStupid in forum New To JavaReplies: 9Last Post: 04-28-2011, 09:22 AM -
Help w/ java programming assignment counting number of spaces
By clemsontigers in forum New To JavaReplies: 9Last Post: 03-06-2011, 04:00 AM -
help with perfect squares
By AmplifiedKid in forum New To JavaReplies: 1Last Post: 09-19-2009, 08:44 PM -
Efficient Perfect Number
By Lite3864 in forum New To JavaReplies: 4Last Post: 11-23-2008, 02:07 AM -
Way to Java Perfect
By Javaisinmyblood in forum New To JavaReplies: 1Last Post: 02-07-2008, 12:28 AM
Bookmarks