Support LQ: Use code LQ3 and save \$3 on Domain Registration
 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, 06: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, 09: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, 01: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(); } } }