开发者

Perfect numbers 1 to n

Write an algorithm that prints perfect numbers from 1 to n. To determine if a number is per开发者_Go百科fect add up all the factors of the number that are less than the number. If the sum is equal to the number, it is perfect.

import java.util.Scanner;

public class Assign_6 {

    public static void main(String[] args){

        int num,number,sum=0,factor;

        System.out.print("Enter Number");
        Scanner keyboard = new Scanner (System.in);

        number=keyboard.nextInt();

        for (num=1;num<number;num++){
            for(factor=1;factor<number;factor++){
                if(num%factor==0){
                    sum= sum+factor;
                }
                if(sum==num){
                    System.out.println(sum);
                }
                sum=0;
            }

        }
    }
}

Output: 24

Nothing prints out. Don't know whats wrong. Where am I going wrong? I can only use while, for, and else-if statements.


Print out the sum for every number (and not just when sum==number) and you'll be able to figure it out.

(Two clues: correct indentation helps find matching { } pairs quickly. And it pays to give your variables descriptive names.)


There are two problems in your code:

  1. The following code should use a less than or equal to comparison, otherwise, you miss the n, only do 1 to (n-1)

    for (num = 1;num < number;num++){
    

    It should be the following code.

    for (num = 1;num <= number;num++){
    
  2. The following code is inside of for(factor=1;...), but it should be outside of the loop. Otherwise, you actually check & clear sum, for each factor.

    if(sum==num){System.out.println(sum);} sum=0;} 
    


Trace through your program on paper. There is an issue with your sum variable.


It's very easy when you are starting out coding to confuse your loops, since in this homework you have 2 for loops and they do different things. As a hint, the reason your program is not working is because you have misplaced stuff from one for loop to the other.

You are in the right direction, but make sure you are able to think about those for loops separately. That is, what is the inner loop's intended purpose? What is it actually doing? What is the outer loop's intended purpose, and what is it actually doing? It looks like in your inner loop you are trying to sum up factors for num. Is it actually just summing up the factors, or are there some side effects/errors you overlooked?


Try this code it will help you:

import java.util.Scanner;

public class Test
{
   public static void main (String[] args)
   {
       Scanner in = new Scanner(System.in);
       int n = in.nextInt();
       for(int j=1; j<n; j++)
       {
           int counter = 0;
           for (int i = 1; i < j; i++)         
           {              
               if(j%i ==0)
               {
                   counter = counter + i;
               }
           }
           if(counter == j)
               System.out.println(j);//here you can print j or counter
       }
   }
}


public class PerfectNumber {
public static void main(String args [])
{

    perfectNumber(6,200);
}

static void perfectNumber(int num1, int num2){

    int i; int j; int sum=0;

     for (i=num1;i<=num2;i++){
            for(j=1;j<=i/2;j++){
                if(i%j==0){
                    sum= sum+j;
                }

            }
            if(sum==i){
                System.out.println("The perfect number between two numbers: "+sum);
            } 
            sum=0;
        }


    }

}


import java.util.*;
    class PerfectNumber

{

public static void main(String[]args)
{
    System.out.println("Enter the number upto which you want to print the perfect number");
    Scanner scan=new Scanner(System.in);
    int n=scan.nextInt();
    for(int i=1;i<=n;i++)
    {
        int sum=0;
        for(int j=1;j<=i;j++)
        {
            if(i%j==0)
            sum=sum+j;
        }
        if(sum==(2*i))  //checks for perfect number
        System.out.println(i);
    }

    }
}


#include<iostream>
using namespace std;
int main()
{
  int start,end;
  cin>>start>>end;
  for(int j = start;j<=end;j++)
  {
    int div=0;
   for(int i =1;i<j;i++)
  {
    if(j%i == 0)
    {
      div = div+i;
    }
  }
    if(div==j)
    {
      cout<<j<<" ";
    }
  }
 return 0; 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜