Homework Help

This is a discussion on Homework Help within the C++ Programming forums, part of the General Programming Boards category; I am new to C++ programming, and have this homework where I need to find all the perfect numbers to ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    5

    Homework Help

    I am new to C++ programming, and have this homework where I need to find all the perfect numbers to 1000.

    I have worked on this problem for a while now, and really trying to get it figured out, but I'm doing something wrong. I was wondering if someone could turn my head as to what it is.

    Code:
    int    testNum = 2; //the number to be tested to see if its perfect - i started it ith 2 since the perfect number must be the sum of all divisors excluding itself.
    	int testDiv; //this sets test divisors to see if it goes into the number
    	int accum = 0; // sums the divisors to see if they equal the test number
    	int counter = 2; // count started at 2 siunce I know that there are none at 1 
    
    	while( counter < 1000)
    	{
    		for (int testDiv = 1; testDiv < testNum; testDiv++)
    		{
    			if(testNum % testDiv == 0)
    			{
    				accum += testDiv;
    			}
    
    			if (accum == testNum)
    			{
    			    cout << testNum << " is a perfect number less
    than 1000." <<endl;
    			}
    			
    		}
    
    		
    	
    
    		counter++;
    		testNum++;
    This is just a snippet of the code in question. Thanks in advance for any help you can give.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > int accum = 0;
    Put this inside your while loop

    > if (accum == testNum)
    Move this outside your for loop

    Perhaps....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    			    cout << testNum << " is a perfect number less
    than 1000." <<endl;
    And put that all on one line, or it won't compile. Or you can split it:
    Code:
    cout << "hello world";
    
    cout << "hello "
        "world";
    
    cout << "hello \
    world";
    
    cout << "hello "
        << "world";
    One of those should do.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 03:39 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21