Thread: help in find perfect numbers

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    75

    help in find perfect numbers

    i want to print first n perfect numbers. when i run it nothing prints
    here is code

    Code:
    #include<stdio.h>void main()
    {
    int i=1,n,sum=0,count=1,j;
    printf("HOw many no u want to print");
    scanf("%d",&n);
    while(count<=n)
      {
          i++;
        for(j=1;j<i;j++)
        if(i%j==0)
        sum=sum+j;
        if(sum==i)
        {
            printf("%d",sum);
            count++;
        }
    }
    }

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Hmm, don't perfect numbers involve summing all the integer divisors? I.e. I think your if (sum == i) is probably in the wrong place. Edit: well, now that I look at it, it's not in the incorrect place (fix that formatting!) What is your loop doing? Calculating the aliquot sum of what (it looks like it's only going to do it for 1)? If so then everything looks right I guess -- to a point. Try adding a \n to your printf().

    Edit:
    Also:
    a) You will benefit from indenting your code correctly;
    b) void main() is not a valid declaration for main

    Edit: You should reset sum to 0 before each iteration of the while loop
    Last edited by Hodor; 12-06-2015 at 05:13 AM.

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    75
    Code:
    tell me what is the code exactly
    where should i place sum=0 and i think i have placed brckets {} in wrong postion. so the program is not running. but i tried for 4 hrs to make program run. still nothing prints

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Why don't you then try starting from a piece of code that works, and work your way up from there?

    ... You tried for 4 hours?! Try more!!! Once, I pulled an all-nighter, 24+ hours trying to figure out why my code wouldn't work! Non-stop!! The only thing I'd eaten/drunk during that was coffee, water and 6 crackers!
    ( Well, ok, maybe it's just me, but I can't relax when I can't find the bug )
    Devoted my life to programming...

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I haven't compiled your program.

    How many times will the loop below be executed?

    Code:
    for(j=1;j<i;j++)
    Given that 'j' is 1 and 'i' is also 1?

    Edit: I'll give you a hint because you're trying (unlike some other people asking questions).

    Your current code is, effectively, the same as:

    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int i=1,n,sum=0,count=1,j;
        printf("HOw many no u want to print");
        scanf("%d", &n);
        while(count <= n)
        {
            i++;
            if (sum == i)
            {
                printf("%d",sum);
                count++;
            }
        }
        return 0;
    }
    Why would this be the case? When will the condition if (sum == i) evaluate to true?

    Why does the while loop, apparently, never end (unless the number you enter for 'n' is 1 (and only 1)? And if this is the case, your code is, effectively, the same as:

    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int i=1,n,sum=0,count=1,j;
        printf("HOw many no u want to print");
        scanf("%d",&n);
        return 0;
    }
    Last edited by Hodor; 12-06-2015 at 09:53 AM.

  6. #6
    Registered User
    Join Date
    Aug 2015
    Posts
    75
    Quote Originally Posted by Hodor View Post
    I haven't compiled your program.

    How many times will the loop below be executed?

    Code:
    for(j=1;j<i;j++)
    Given that 'j' is 1 and 'i' is also 1?

    Edit: I'll give you a hint because you're trying (unlike some other people asking questions).

    Your current code is, effectively, the same as:

    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int i=1,n,sum=0,count=1,j;
        printf("HOw many no u want to print");
        scanf("%d", &n);
        while(count <= n)
        {
            i++;
            if (sum == i)
            {
                printf("%d",sum);
                count++;
            }
        }
        return 0;
    }
    Why would this be the case? When will the condition if (sum == i) evaluate to true?

    Why does the while loop, apparently, never end (unless the number you enter for 'n' is 1 (and only 1)? And if this is the case, your code is, effectively, the same as:

    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int i=1,n,sum=0,count=1,j;
        printf("HOw many no u want to print");
        scanf("%d",&n);
        return 0;
    }
    give me the code for program plz

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by san12345 View Post
    give me the code for program plz
    Answer my questions first and then we can move forward.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find Perfect Number generator stops at 6
    By JonathanS in forum C Programming
    Replies: 9
    Last Post: 01-17-2012, 07:12 PM
  2. Perfect Numbers
    By BeldenML in forum C Programming
    Replies: 19
    Last Post: 10-21-2011, 11:52 AM
  3. Perfect Numbers
    By iLike in forum C++ Programming
    Replies: 2
    Last Post: 10-18-2009, 12:27 PM
  4. perfect numbers
    By budala in forum C Programming
    Replies: 3
    Last Post: 08-08-2009, 04:16 PM
  5. perfect numbers
    By tomahawker in forum C++ Programming
    Replies: 14
    Last Post: 12-11-2005, 03:50 PM