Thread: perfect number

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    perfect number

    My program works fine and listed the 3 perfect numbers under 1000, but i need help with listing the factors of the perfect numbers. Can someone please help me out with this?


    Code:
    #include <iostream>
    using namespace std;
    void main()
    {
        int n = 1000,sum = 0;
    	int i;
    
               cout << "Searching for perfect number less than 1000..." << endl;
           for(int num = 2; num <= n; num++)
        {
               sum = 0;
    
           for(i = 1; i < num; i++)
        {
           if(num%i==0)
    	  
               sum+=i;
    	   
        } 
           if(sum == num)
              cout <<  num << " Is perfect number" << endl;
    	
        }
    }
    Last edited by mallyg34; 04-01-2010 at 11:14 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You find the factors once, you can do it again.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Quote Originally Posted by tabstop View Post
    You find the factors once, you can do it again.
    I don't understand.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mallyg34 View Post
    I don't understand.
    You have a whole section of code devoted to finding the factors of a number. If you did it in one place, presumably you can also run that same code in another place to find the factors of a number (again).

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Can't I someway put a "cout" statement in a loop to get the factors instead of make the code extra long.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    You may want to fix the indentation so people will have an easier time reading your code. It may even help you discover a bug or two.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Quote Originally Posted by mallyg34 View Post
    Can't I someway put a "cout" statement in a loop to get the factors instead of make the code extra long.
    It depends on how you have to display it.

    As was stated, you are already finding factors, so storing the found factors in an array/vector for later display would be the route that I would take.
    (this can be done in 3, maybe 4 lines).

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Quote Originally Posted by Cogman View Post
    It depends on how you have to display it.

    As was stated, you are already finding factors, so storing the found factors in an array/vector for later display would be the route that I would take.
    (this can be done in 3, maybe 4 lines).
    We havent learned arrays yet in this language so im sure I cant use it until we get to it. This how the output is suppose to look:

    6 is a perfect number
    Factors: 1, 2, 3

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    242
    the code only becomes barely longer by using the same methods you used in one of the blocks you have.
    and i agree totally with cyberfish about the need for improvement with regard to indentation practices.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It seems this question has already been answered here.

    But honestly, did you write your code in a sleep? It is already summing the factors, so how on earth is it possible that you can't figure out how to find the factors?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Perfect number and divisors
    By Lissa in forum C Programming
    Replies: 31
    Last Post: 10-24-2008, 01:36 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  5. Perfect number
    By TheSki in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2001, 04:34 PM

Tags for this Thread