Help please please please!!!

This is a discussion on Help please please please!!! within the C Programming forums, part of the General Programming Boards category; I am new to C programming and trying to build a c program that can calculate perfect number from 1 ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    Question Help please please please!!!

    I am new to C programming and trying to build a c program that can calculate perfect number from 1 to 10000, perfect number is like....1+2+3=6, 1+2+4+7+14=28....

    help please please please!!!

    here are my codes

    Code:
    #include <stdio.h>
    #include <math.h>
    #define max_n 7.0
    
    int 
    main(void) 
    {
        double n;
    	int number;
    	for (n=0;n<max_n;n+=1)
    	{
    	
    	number = pow(2.0,n-1.0)* (pow(2.0,n)-1.0);
    	
    	if (number>1 && number <10000)
    	{
    	
    		printf("The perfect number is &#37;d.\n", number);
    	}
    	else
    	{
    		printf("System Error");
    	}
        return (0);
    	}
    }
    Last edited by erere319; 03-28-2008 at 07:47 AM.

  2. #2
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    I'm still not sure I get the concept of a "perfect" number. What exactly are you trying to do here?

    Edit: Oh! I think I see now . . .

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define MAX_N 7.0
    
    int main(void) 
    {
            double n;
    	int number;
    	
    	for (n=0; n<MAX_N; n += 1)
    	{
    		number = pow(2.0,n-1.0)* (pow(2.0,n)-1.0);
    	
    		if ((number > 1) && (number < 10000))
    		{
    			printf("The perfect number is &#37;d.\n", number);
    		}
    		else
    		{
    			printf("System Error");
    		}
    	}
    	
    	return 0;
    }
    Start by indenting your code properly. And move the return 0 out of the for loop.

    Here is my output I am getting:
    Code:
    System ErrorSystem ErrorThe perfect number is 6.
    The perfect number is 28.
    The perfect number is 120.
    The perfect number is 496.
    The perfect number is 2016.
    Last edited by JDGATX; 03-28-2008 at 07:59 AM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so a few things that are wrong with your code:
    1. You return inside the for-loop.
    2. You do NOT return at the end of main [which of course matters little if you return "early" every time].
    3. It is not actually giving perfect numbers [after fixing the above] since it doesn't make sure that n is a prime number (in fact, it needs to be a Mersenne prime) . See: http://en.wikipedia.org/wiki/Perfect_number

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

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