Perfect Numbers

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

Page 1 of 1

2 Replies - 18893 Views - Last Post: 06 October 2007 - 08:40 PM Rate Topic: -----

#1 prezident  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 06-October 07

Perfect Numbers

Posted 06 October 2007 - 02:36 AM

[size=4]
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: 06 October 2007 - 01:25 PM


Is This A Good Question/Topic? 1

Replies To: Perfect Numbers

#2 chuck87  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 07-September 07

Re: Perfect Numbers

Posted 06 October 2007 - 04:54 AM

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
Was This Post Helpful? 0
  • +
  • -

#3 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: Perfect Numbers

Posted 06 October 2007 - 08:40 PM

search the forums we did a program like this a couple weeks ago.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1