February 06, 2010

+Perfect Numbers+Java Program

/**
* Find the perfect numbers from 1 to 32767
*
* @author (Oliver Tamboboy)
* @version (November 8,2009)
*/
import javax.swing.*;
public class PerfectNumbers
{
public static void main (String[]args)
{
String outStr="The perfect numbers from 1-32767 are:";
int k=1,sum,k2;
JOptionPane.showMessageDialog(null,outStr,"Perfect Numbers",
JOptionPane.INFORMATION_MESSAGE);
do
{
k++;
sum=0;
for(k2=1;k2<=k/2;k2++)
{
if(k%k2==0)
sum=sum+k2;
}
if (sum==k)
JOptionPane.showMessageDialog(null,sum,"",JOptionPane.ERROR_MESSAGE);
}while(k<=32767);
}
}

No comments:

Post a Comment