Monday 3 December 2012

PROGRAM IN JAVA TO FIND PERFECT NUMBERS


import java.util.Scanner;

public class PerfectNumber {

    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        System.out.print("How many perfect numbers do you want to print = ");
        String str = sc.next();
        int limit = Integer.parseInt(str);
        int checkLimit = 1;
        
        for( int i = 2; i < Integer.MAX_VALUE; i++ )
        {
            boolean bool = perfectNumber(i);// passing value in function
            
            if(bool == true)
            {
                checkLimit++;
            }
            
            if(checkLimit > limit)
            {
                break;
            }
        }
    }
    
    public static boolean perfectNumber(int input){
        
        boolean bool= false;
        int size = 2000;
        int[] arr = new int[size];
        int counter = 0;
        int divisorSum = 1;
        
        for(int i = 2; i < input; i++)
        {
            if(input % i == 0)
            {
                arr[counter] = i;
                divisorSum += arr[counter];
                counter++;
            }
        }
        
        if(divisorSum == input)
        {
            System.out.print(input+" is a perfect number and divisors's sum = ");
            System.out.print("1");
            for(int i = 0; i < size; i++)
            {
                if( arr[i] == 0 )
                {
                    continue;
                }
                System.out.print("+"+arr[i]);
            }
            System.out.println("");
            bool = true;
        }
        return bool;
    }
}


  • The output of this progarm will be like shown in an image below.



HOW TO ADD JAVADOC ( OFFLINE ) IN ECLIPSE

 
  1. Firstly, you have to download javadoc api from Oracle's website. This file may be in compressed format having extension with .rar or .zip. Below is the link from where you can download it.
  2. http://www.oracle.com/technetwork/java/javase/documentation/java-se-7-doc-download-435117.html
  3. Now place this compressed file wherever you want.
  4. Open eclipse and navigate to "window" in menu bar and then click on "prefrences"
  5. Now do exactly what i have done in an image below.
  • Now click on installed jre, in my case its C:\Program... and click "edit".
  • Now select jre system libraries by pressing Cntrl+a to select all libraries and click javadoc location.
  • Now do exactly what i showed you in an image below.
  • Keep in mind that you will have to give both paths.
  • One is the archive path, that is simply your file's path.
  • Second one is that path to the folder "api" that is placed in archive,
Note: If you do not give the second path then it might happen that javadoc help might not show in the tooltip while writing programs in eclipse IDE.