Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2004
    Posts
    1

    Exclamation Unanswered: C program help!!!

    Hi, I was told to make a perfect number program in C. We began the semester less than a month ago, so we are using nothing very complicated, but I am having trouble getting it to work. It is supposed to have no input..just output and list all perfect numbers between 1 and LIMIT, which equals 10,000. Can someone show me how I could possibly do this? Your help is greatly appreciated!!!

    Thanks,
    Jedijacob

  2. #2
    Join Date
    Sep 2004
    Posts
    2
    There probably is a better way to do it, but the only one I can think of is the following:

    int isPerfect(int n)`
    {
    sum=1;
    for (int i=2;i<n;i++)
    if (n%i==0) sum+=i;
    return sum==n;
    }

    void main(void)
    {
    #define LIMIT 10000
    for (int i=1;i<=LIMIT;i++)
    if (isPerfect(i)) printf("%d\t",i);
    }

    I haven't tested it (sorry, I'm not able to test it now), and it is obviously a time consuming algorithm. I do believe it works. Hope it helps.

  3. #3
    Join Date
    Oct 2004
    Posts
    2
    Just make sure you don't use void main(), since main never should be void. main is 'special', and the same rules, regarding void, does not apply to main. On some special systems void main is allowed, but a good thumb rule is to always define a return value, even though you may not use it. Under the C99 standard, int main(void) is actually the only acceptable (together with argc, argv of course), but the following code is ok:
    Code:
    int main() {
      return 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •