Wednesday, July 8, 2015

Perfect Number Generator in Java

In this article I would like to share with you a sample program that I wrote in my spare time I called this program Perfect Number Generator in Java. According to Wikipedia.org a perfect number is defined as in number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum).

What does our program will do is to ask the user to enter a number as our range from the given number our program will list down the numbers that is belong to perfect number. By the way I am using Netbeans as my text editor in this program a good text editor that is very user friendly to use and best of all it is free to download from the Internet because it is open source.

The program is very simple and easy to understand. I hope you will find my work useful in learning how to program in Java. If you have some questions please send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com.


People here in the Philippines can reach me at my mobile number 09173084360.


Thank you very much and Happy Programming.


Sample Program Output


Program Listing

import java.util.Scanner;  

class perfect_number
{
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
       System.out.print("\t\t    PERFECT NUMBER GENERATOR ");
       System.out.print("\n");
       System.out.print("\nKindly enter a Number ? :");
        long range_number=input.nextLong();
        System.out.println("\nThe perfect numbers from 1 and "+range_number+" are:");
        System.out.println();
        for(int b=1;b<range_number;b++)
        {
            int sum = 0;
            {
                for(int a=1;a<b;a++)
                {
                    if(b%a == 0)
                    {
                        sum+=a;
                    }
                }
                if(sum == b)
                {
                   
                    System.out.print(" "+ b + " ");
                }
            }
        }
       System.out.println("\n");
       System.out.println("\t\t END OF PROGRAM");     
       System.out.println("\n"); 
    }
       
}





No comments:

Post a Comment