View Single Post
  #1 (permalink)  
Old 01-30-2008, 08:10 AM
jvasilj1 jvasilj1 is offline
Member
 
Join Date: Jan 2008
Posts: 26
jvasilj1 is on a distinguished road
students needs help
i have created java code to give me perfect numbers, and it works...but now im hung up on modifying the main to give me AMICABLE NUMBERS. Any ideas???????
HERE IS MY PERFECT.JAVA CODE
----------------------------------------------
// Find all perfect numbers less than
// or equal to then entered upper bound.

import javax.swing.JOptionPane;

public class Perfect
{
public static void main(String[] args)
{
int upperBound, n, factor, sum;

upperBound = Integer.parseInt(JOptionPane.showInputDialog(
"Enter upper bound for perfect number search"));

for(n = 2; n <= upperBound; n++)
{
sum = 0;
for(factor = 1; factor <= n / 2; factor++)
if (n % factor == 0)
sum += factor;

if (n == sum)
System.out.println(n);
}
}
}

// Prompt of input dialog:
Upper bound for perfect number search:

// Sample input to input dialog:
10000

// Output in Terminal Window:
6
28
496
8128
Reply With Quote
Sponsored Links