Welcome to Dream.In.Code
Become a Java Expert!

Join 149,457 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,134 people online right now. Registration is fast and FREE... Join Now!




Perfect Numbers

 
Reply to this topicStart new topic

Perfect Numbers, algorithm that will check if a number supplied by the user is a perfec

prezident
6 Oct, 2007 - 01:36 AM
Post #1

New D.I.C Head
*

Joined: 6 Oct, 2007
Posts: 1


My Contributions
[size=4]
CODE
public class PerfectNo
{
        
        public void perfectNoGenerator()
        {      
            System.out.println( "please enter a number to check :  ");
            Scanner keyboard = new Scanner(System.in);
            int no = keyboard.nextInt();
        
                int counter = 0;
                //int no = 1;
                int sum;
                
                
                do
                {
                    no = no + 1;
                    sum = 0;
                    
                    for (int i =1;i<=(no/2);i++)
                    {
                        
                        
                        if(no % i== 0)
                        {
                            
                            sum = sum + i;
                        }
                        
                        
                        
                    }
                    
                    
                    if(sum ==no)
                    {
                        counter = counter +1;
                                        
                        System.out.println( sum +" is a perfect number ");        
                    }
                                      
                                                   
                    
                    
             } while(counter< 5);
            
         }  
                
        

        public static void main(String [] args)
        {       PerfectNo jobj = new PerfectNo();
                jobj.perfectNoGenerator();
        }
}


This post has been edited by PennyBoki: 6 Oct, 2007 - 12:25 PM
User is offlineProfile CardPM
+Quote Post

chuck87
RE: Perfect Numbers
6 Oct, 2007 - 03:54 AM
Post #2

D.I.C Head
**

Joined: 7 Sep, 2007
Posts: 65


My Contributions
As you mention in the topic this program checks if a number provided by the user is a perfect number.What I got after executing it is completely different
When I provided numbe 10 the output was
28 is a perfect number
496 is a perfect number
8128 is a perfect number

When provided number 3
6 is a perfect number
28 is a perfect number
496 is a perfect number
8128 is a perfect number


If I am not wrong this program shows numbers that are perfect numbers and also greater than the number provided.
So it doesn't check if a number provided is a perfect one


User is offlineProfile CardPM
+Quote Post

sl4ck3r
RE: Perfect Numbers
6 Oct, 2007 - 07:40 PM
Post #3

D.I.C Head
Group Icon

Joined: 22 Sep, 2007
Posts: 119


Dream Kudos: 25
My Contributions
search the forums we did a program like this a couple weeks ago.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 01:32PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month