Share your knowledge at the LQ Wiki.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org on JAVA: finding perfect numbers
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 10-02-2003, 05:17 PM #1 randomx Member   Registered: Feb 2003 Location: Hawaii Distribution: Debian Posts: 130 Rep: on JAVA: finding perfect numbers Hi all; I learn by studying solved examples. This code checks for perfect numbers. A natural number n is perfect if it equals the sum of * all its divisors (including 1 but not including itself). * For example 6 = 1+2+3 is perfect 28 = 14 + 7 + 4 + 2 + 1 is perfect too. .. now...going beyond my knowledge, How do I make the user type whatever number and then the program decides whether it's perfect or not? maybe I'm just stuck on the for-loop conditions.... right now the program searches by itself the perfect numbers from 0 to 100000. here is the code. Any ideas? class Perfect { public static boolean isPerfect( int n ) { if (n <= 0) return false; int sum = 0; for (int i=1; i n) return false; } } return sum == n; } public static void main( String args[] ) { int count = 0; int max = 100000; for (int n=1; n<=max; n++) { if (isPerfect(n)) { System.out.println("Found perfect number: "+n); count = count+1; } if (n % 10000 == 0) { System.out.println("Searched up to: "+n); } } System.out.println("Found "+count+ " perfect numbers between 1 and "+max); } }
 10-02-2003, 08:32 PM #2 Looking_Lost Senior Member   Registered: Apr 2003 Location: Eire Distribution: Slackware 12.0, OpenSuse 10.3 Posts: 1,120 Rep: This'll read in a valid integer from negative to positive and fail gracefully otherwise exiting the program. You can mess about and make it do something else though but 'tis an example Code: ``` import java.io.*; int number=0; BufferedReader buffRead=new BufferedReader(new InputStreamReader(System.in)); //set up reading in standard input try { String temp=new String(buffRead.readLine()); // read a line from standard input temp=temp.trim(); // Get Rid of trailing and leading whitespace if any number=Integer.parseInt(temp); }catch(IOException e) { System.out.println("Error reading data"); System.exit(1); } catch(NumberFormatException nfe) { System.out.println("Invalid Number"); System.exit(1); }```
 10-05-2003, 12:36 PM #3 randomx Member   Registered: Feb 2003 Location: Hawaii Distribution: Debian Posts: 130 Original Poster Rep: thanks bro. my implementation here I figured it out. I'm posting my implementation here. Maybe somebody else might need it in the future as a reference. randomX public class PerfectNumber { public static void main(String [] args) { int numberEntered, iteration, add; System.out.println(); System.out.print("Enter a positive integer: "); numberEntered = ; while (numberEntered <= 0) { System.out.println(); System.out.print("Only positive integers are accepted. Please try again: "); numberEntered = ; System.out.println(); } add = 0; //initialization for (iteration = 1; iteration < numberEntered; iteration++ ) { if (numberEntered % iteration == 0 ) // check if it's even { add = add + iteration; } } if (add == numberEntered) //compares the result to what the user typed in { System.out.println(); System.out.println("" + numberEntered + " is a perfect number." ); System.out.println(); } else { System.out.println(); System.out.println("" + numberEntered + " is NOT a perfect number. Sorry." ); System.out.println(); } } }

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post hanasi SUSE / openSUSE 5 08-12-2005 08:47 AM Sephiroth Linux - Software 0 03-13-2004 08:14 PM spyghost Programming 8 09-06-2003 06:21 PM pyropenguin Linux - Newbie 2 09-19-2002 01:02 PM pyropenguin Linux - Software 2 09-15-2002 07:55 AM

LinuxQuestions.org

All times are GMT -5. The time now is 09:41 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -