Thursday, August 11, 2011

0

C Program Language (Perfect Numbers)

  • Thursday, August 11, 2011
  • RJ
  • Share
  • In this program you will be told on how to sold problem in C program language which the perfect and not perfect numbers. See the screen example below.
    After you've seen the expected output i will show you the codes on how to get the c program perfect numbers. Check out the codes.

    #include<stdio.h>
    #include<conio.h>
    
    long perfectNum(long num);
    
    int main()
    {
        int limit, c = 0;
        long num;
            printf("Enter how many counts: ");
            scanf("%d", &limit);
                    
                for( c = 1; c <= limit; c++ ){
                    printf("\nEnter Numbers: ");
                    scanf("%d", &num);
                        if( num <= 0 )
                            return 0;
                        if( perfectNum(num) )
                            printf("Perfect\n");
                        else
                            printf("Not Perfect\n");
                }
                        
        getch();
    }
    
    long perfectNum(long num)
    {
        int a = 0;
        long sum = 0;
        
            for( a = 1; a < num; a++ ){
                if( num % a == 0 )
                    sum += a;
            }
                if( sum > num || sum < num )
                    return 0;
            return 1;
    }
    
    Perhaps you are wandering why 6, 496 and 28 are perfect numbers. So the given example below will tell us why it is called a perfect number.
    
    
    6 = 1 + 2 + 3
    28 = 1 + 2 + 4 + 7 + 14
    496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248
    
    
    Thanks for reading this post. :)
    
    

    0 Responses to “C Program Language (Perfect Numbers)”

    Post a Comment

    Subscribe