Thread: Find Perfect Number generator stops at 6

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    Find Perfect Number generator stops at 6

    This is a perfect number generator, when you start from 0 to 100 it should find 6 and 28, but for some reason it only recognizes 6 as a perfect number. Why doesn't it output 28? Danke!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int check;
    int isAFactor(int i, int o);
    
    
    
    int main()
    {
    
        int startval;
        int endval;
        int o;//outer loop
        int i = 1;//inner loop
    
        int check2 = 0; //if remains 0, no perfect numbers.
    
    
        int add = 0;
    
    
        printf("Enter starting integer: ");
        scanf("%d", &startval);
        printf("\nEnter ending integer: ");
        scanf("%d", &endval);
    
        o = startval;
    
        for(;o<=endval;o++)
        {
            for(; i<=o/2; i++)
            {
                check = isAFactor(i,o);
    
                if (check == 0)
                {
                    add = add + i;
                }
    
            }
    
            if(add == o && o!=0)
            {
                printf("\n%d is a perfect number.\n", o);
                check2 = 1;
            }
    
        }
    
            if(check2 ==0)
            {
                printf("\nNo Perfect Numbers found between %d and %d.", startval, endval);
            }
    
    
        return 0;
    }
    
    
    int isAFactor(int i, int o)
    {
        check = o%i;
        return check;
    }
    My Ctrl+S addiction gets in the way when using Code Blocks...

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,649
    My guess is that you need to have a valid starting value of 1 for i in this loop.
    Code:
    for(; i<=o/2; i++)
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    When I do that for some reason it doesn't find anything. I think I', asking a bit much, I did put alot of effort and thought in the program. I'll put some more in it :P
    My Ctrl+S addiction gets in the way when using Code Blocks...

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,649
    You also need to reset "add" at some point in your code.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    had to make add = 0 again doh!
    My Ctrl+S addiction gets in the way when using Code Blocks...

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    oopsy you answered it :P thanks for your time!
    My Ctrl+S addiction gets in the way when using Code Blocks...

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
        o = startval;       
       for(;o<=endval;o++)     
          {
             for(; i<=o/2; i++)
    The above is a *real bad idea* .... put the initializer for the loop *in the loop*.
    What is the value of i the second time through the loop?

    I've seen it on here a number of times lately, from a number of different posters.
    I sincerely hope there's not some fool teacher out there advocating it!

    Code:
    for (o = startval; o < endval; o++)
      for ( i = 0; i < o /2; i++)

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    No, I teach myself. But the reason I did that is for some reason it doesn't work when I put it in there. I'm going to check it out now that it is working.
    My Ctrl+S addiction gets in the way when using Code Blocks...

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Well, I changed it to while() :S I'll put it in for()'s and see if there is any change
    My Ctrl+S addiction gets in the way when using Code Blocks...

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    changed it to for() it works

    Thanks all for your help! it is much appreciated! Sorry for anyone that leeps looking at this post bc of my replies :/
    My Ctrl+S addiction gets in the way when using Code Blocks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. perfect number
    By mallyg34 in forum C++ Programming
    Replies: 9
    Last Post: 04-03-2010, 02:20 PM
  2. What is perfect number
    By IPCHU in forum C Programming
    Replies: 5
    Last Post: 12-08-2006, 11:23 AM
  3. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM
  4. Prime Number stops after 29, but why?
    By Daveo in forum C Programming
    Replies: 22
    Last Post: 09-17-2004, 10:55 AM
  5. Perfect number
    By TheSki in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2001, 04:34 PM