English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

write a program that would display first 5 positive perfect numbers. A perfect number is a number whose sum of all its divisors except itself is equal to that number. For example, 6 is a number because the sum of all its divisors except itself (1 2 3) is equal to 6.

2007-09-12 02:49:39 · 4 answers · asked by herbert v 1 in Computers & Internet Programming & Design

4 answers

Do you own homework, but here is a hint for problems like that,

First do it mathematically, so you have you formulas
then dump that formula in a function that will accept a parameter of the number entered by the user

hope this helps

2007-09-12 03:04:21 · answer #1 · answered by Arkane Steelblade 4 · 1 0

To respect the above comment i won't do everything for you ... I've made a c++ version of the program so you won't have to think about the algorithm you will only have to convert it to java(java is c based so it shouldn't be too hard)
int num,buff=0;
printf("input a number : ");
scanf("%d",&num);
for(int i=num-1;i > 0;i--){
buff += i;
}
if(buff == num){
printf("%d is a perfect number",num);
}else{
printf("%d is not a perfect number",num);
}


ill give you some tips....

import java.io.*;//so you can use the functions below
BufferedReader stdin = new BufferedReader( new InputStreamReader (
System.in));
int i = Integer.parseInt(stdin.readLine());
System.out.println("You've entered: " + i);

For Loop is similar to c++

2007-09-12 03:09:24 · answer #2 · answered by ko_cjun 2 · 0 0

this was tricky but try this for the first 3 perfect numbers:

public class PerfectNumber
{
public static void main(String[]args)
{
int sum=0, x=0;
for(int num=1;num<1000;num++)
{
for(int factor=1;factor {
x=num%factor;
if(x==0)
sum=sum+factor;
}
if(sum==num)
{
System.out.println("\n\n"+num+":");
System.out.println("\nThe factors are: ");

for(int factor=1;factor {
x=num%factor;
if(x==0)
System.out.println(factor);
}
}
sum=0;
}
}
}

2007-09-12 17:30:43 · answer #3 · answered by Luke V 3 · 0 0

Sounds like you want us to do your homework for you. If you have a specific question about a problem implementing, please ask about that. You are in school to learn though so don't ask us to complete your entire assignment.

2007-09-12 02:54:16 · answer #4 · answered by Jim Maryland 7 · 1 0

fedest.com, questions and answers