GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Oct-2008, 17:07
aedude94 aedude94 is offline
New Member
 
Join Date: Sep 2008
Posts: 5
aedude94 is an unknown quantity at this point

Perfect numbers within a range


Ok, so I have to create this program that asks for a range input and then finds and outputs all the proper numbers within that range and tells how many proper numbers were found (as if the person can't count, haha). Anyways, I wrote a function to do just that and don't understand why it does not work at all! Does anyone have any ideas to how I would go about doing this? Thanks!
  #2  
Old 08-Oct-2008, 17:12
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: perfect numbers within a range


Quote:
Originally Posted by aedude94
Anyways, I wrote a function to do just that and don't understand why it does not work at all! Does anyone have any ideas to how I would go about doing this?
Post your code. No one can debug your code without actually seeing it.

When posting code, place a [cpp] tag before the code, & [/cpp] tag afterwards. This will preserve indentation & color code keywords & identifiers.
  #3  
Old 08-Oct-2008, 17:14
aedude94 aedude94 is offline
New Member
 
Join Date: Sep 2008
Posts: 5
aedude94 is an unknown quantity at this point

Re: perfect numbers within a range


CPP / C++ / C Code:

int sum = 0;
		int perfectStart = start;
		int perfectNum = 1;
		while (perfectStart <= end){
			while (perfectNum < perfectStart){
				if ((perfectStart % perfectNum) == 0){
					sum = sum + perfectNum;
					perfectNum++;
				}
				
			}
        
		    if (sum == perfectStart){
					cout << perfectStart << " is a perfect number" << endl;
			}
			perfectStart++;
			perfectNum = 1;
		}
	}

  #4  
Old 08-Oct-2008, 17:21
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: perfect numbers within a range


Quote:
Originally Posted by aedude94
CPP / C++ / C Code:
while (perfectStart <= end){
Where is end defined?

How do you expect anyone to debug your code when you don't provide anything that can be compiled?
  #5  
Old 09-Oct-2008, 09:28
zalezog zalezog is offline
Junior Member
 
Join Date: Oct 2007
Posts: 33
zalezog will become famous soon enough

Re: Perfect numbers within a range


if perfectstart is your beginning number and endnumber is your limit.then
CPP / C++ / C Code:
//fragment code
sum=0;
while(perfectstart<=endnumber)
{
  sum=0;
  for(i=1;i<=perfectstart/2;++i)
   {
      if(perfectstart%i==0)
      { sum+=i;}
    }   
      if(sum==perfectstart)cout<<perfectstart<<endl;
    ++perfectstart;
}
hope this helps
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Perfect Numbers Corrupt Shadow C++ Forum 9 31-Mar-2008 07:43
recursive function for perfect numbers? cancan C Programming Language 6 19-Nov-2007 12:46
Prime or perfect numbers and output factors buomque C++ Forum 4 26-Oct-2007 10:23
Need help generating a random number Allie C Programming Language 7 09-Nov-2005 23:18
Program that will find the first 4 perfect numbers????? bicknell83 C Programming Language 3 30-Mar-2005 02:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:28.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.