Perfect Number program

This is a discussion on Perfect Number program within the C Programming forums, part of the General Programming Boards category; In the Perfect Number Program, when I run the program I enter a number such for example 6 which is ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    19

    Perfect Number program

    In the Perfect Number Program, when I run the program I enter a number such for example 6 which is a perfect number after I put in the number it says its not a perfect number if I entered a different perfect number like 28, 496, 8128 it still says its not a perfect number. Is there something in my code that prevents from showing the perfect numbers after entering the number?

    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main()
    {
    
         int perfect(int c);
         int a,b,c,i;
    
         printf("Enter a Number: " );
         scanf("%d",&a);c=0;
    
         if(c==a) {
         printf("\nA perfect number\n");
    }
    
         else
         printf("\nNot a perfect Number\n");
    
    
         return  0;
    }
    
    int perfect(int c)
    {
    
           int a,b,i;
    
          for(i=1;i<a;i++)
          {
    
    
         b=a%i;
         if(b==0)
            {
    
            c=c+i;
    
           if(c==a) {
    
           printf("\nA perfect number\n");
           }
           else
                printf("\nNot a perfect Number\n");
           }
           return 0;
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Some problems:

    1) You never call the perfect function. All you have is a prototype of it, and the function, but execution is never going there.

    2) You assign c the value of 0, then if(c==a) it's a perfect number?

    Not the way to do it.

  3. #3
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Cool

    Quote Originally Posted by tiger6425 View Post
    In the Perfect Number Program, when I run the program I enter a number such for example 6 which is a perfect number after I put in the number it says its not a perfect number if I entered a different perfect number like 28, 496, 8128 it still says its not a perfect number. Is there something in my code that prevents from showing the perfect numbers after entering the number?

    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main()
    {
    
         int perfect(int c);
         int a,b,c,i;
    
         printf("Enter a Number: " );
         scanf("%d",&a);c=0;
    
         if(c==a) {
         printf("\nA perfect number\n");
    }
    
         else
         printf("\nNot a perfect Number\n");
    
    
         return  0;
    }
    
    int perfect(int c)
    {
    
           int a,b,i;
    
          for(i=1;i<a;i++)
          {
    
    
         b=a%i;
         if(b==0)
            {
    
            c=c+i;
    
           if(c==a) {
    
           printf("\nA perfect number\n");
           }
           else
                printf("\nNot a perfect Number\n");
           }
           return 0;
    }
    }
    Adak already pointed out the obvious.

    Further examination reveals that "a" was never initialized in your perfect() function, probably
    causing your program to crash. See above in red.
    Last edited by Char*Pntr; 09-03-2010 at 01:40 AM. Reason: left out another unititalzed int a

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Well, it wouldn't crash but it would surely produce false results everytime.
    Devoted my life to programming...

  5. #5
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by Sipher View Post
    Well, it wouldn't crash but it would surely produce false results everytime.
    I realized that after I posted. I should have commented that if perfect() had been called in main,
    the program would crash.
    Last edited by Char*Pntr; 09-03-2010 at 03:14 PM. Reason: I need to remember to preview first before posting!

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    6, 28, 496, and 8128 are not "perfect numbers" because you assigned c to zero. The perfect function is dead code because you declared a prototype but never called it.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    19

    perfect number from a few quotes ago

    I see the red in int perfect() a few quotes ago that a was never initialized which I
    don't understand what it means by not being initialized by int a; which
    is declared in int perfect(), can you show me a example that would intialize
    the variable a that is probably why the program isn't returning the information
    for the perfect numbers?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tiger6425 View Post
    I see the red in int perfect() a few quotes ago that a was never initialized which I
    don't understand what it means by not being initialized by int a; which
    is declared in int perfect(), can you show me a example that would intialize
    the variable a that is probably why the program isn't returning the information
    for the perfect numbers?
    There are more problems than just an uninitialized variable...
    But anyway, to initialize a variable you give it a value when you create it.

    int A = 11;

    Never assume that creation results in 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 07:00 AM
  2. Replies: 11
    Last Post: 10-07-2008, 07:19 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM

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