Thread: Any hint would be appreciated......

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    9

    Question Any hint would be appreciated......

    My son & I are throughly frustrated....we've gotten this far on a program for the perfect numbers between 1 - 100, but have hit many dead ends. So far, the output is "3206" - but in the many attempts have had outputs of 0, 1, blank, and a list of 100 zeros with a 2 at the end. I know that it's something simple, but sometimes the more you mess with something, the harder it is to spot the obvious mistake. If someone would be kind enough to point me in the right direction, so that I can point my kid in the right direction, I would truly appreciate it!

    Code:
     /*This program displays all the perfect numbers in the range of 1-100 */
    
    
    #include<stdio.h>
    #include<math.h>
    #include"genlib.h"
    #include"simpio.h"
    
    
    
     int perfect(int num);
    
     main()
    
    	{
    		int num, p;
    
    		printf ("the perfect numbers in the range of 1 - 100 are:\n\n");
    
    		for (num=2; num<=100; num+=2);  /*all known perfect numbers below the
    													value of 10 to the 300th power are
    													even*/
    
    
    			p =  perfect (num);
    			printf ("%d\n", p);
    
      }
    
      int perfect(int num)
    
    		{
    
    		int i, sum, p;
    
    
    
    			for (i=2; i<num; i++)
    
    				{
    
    				if (num%i == 0)
    						{
    						sum += num;
    						}
    
    					if (sum == num)
    							{
    							sum = p;
    							}
    
    
    
    
    			}
    
    
    				return (p);
    
    		}

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Try removing the semicolon after the for loop

    Code:
    for (num=2; num<=100; num+=2);
    Mr. C: Author and Instructor

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    9
    Thank you....

    Still generates "3206", sigh.......

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When using multiple lines of code in a loop, you must use braces. Example:
    Code:
    for (num=2; num<=100; num+=2)
    {
    	p =  perfect (num);
    	printf ("%d\n", p);
    }
    Otherwise what happens is this:
    Code:
    for (num=2; num<=100; num+=2)
    {
    	p =  perfect (num);
    }
    printf ("%d\n", p);
    See the difference?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    9
    Banging my head against the wall.......

    Yes, I see, LARGE sigh. I wish that I had the right mindset for this, and I wish that I had tackled this at a much younger age. I hate making stupid mistakes like that!

    I think that I have been too focused on the bottom function, trying to figure out why it's generating the funky numbers (now it's a 3206 and 99 "8729"s)... obviously we have something very wrong with the simple mathematics there

    Thank you so much for your help!

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Look in the perfect() function at the variable p. Lets follow this through quickly:

    - You create it with
    >int p;
    - The next thing you use p for is this:
    >sum = p;
    This is a problem because you haven't yet assigned a value to p, so how can you assign p's value to sum?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    27

    hmmm

    Shouldn you try sum == num
    after the loop.
    ANd if im not that wrong you never get a perfect number because
    you start with 2.
    1 is the divisor of all numbers and you never add 1 to sum.
    And it enough to loop to num / 2 because n/(n/2+1) is smaller than two and the number it self is not considered with perfect numbers
    "Can i really learn this? If i cant, why bother?"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Control, HELP appreciated.
    By Astra in forum Windows Programming
    Replies: 7
    Last Post: 01-01-2007, 06:59 AM
  2. hi! could anybody help me, a hint at the least
    By alvarorahul in forum C Programming
    Replies: 1
    Last Post: 07-07-2004, 02:37 AM
  3. Just a HINT
    By Diceman in forum C++ Programming
    Replies: 1
    Last Post: 07-25-2003, 05:27 PM
  4. C++ Programs -- Any input appreciated
    By Kyoto Oshiro in forum Game Programming
    Replies: 0
    Last Post: 02-27-2002, 11:22 PM
  5. I need help..anything will be appreciated
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2001, 10:55 AM