Computer Scientists are Pretty Pessimistic

Saturday 3 October 2015

WAP to find n perfect numbers where n is the input from user.

Sample input : 3
Sample output : 6 28 496


CODE ::

#include <stdio.h>
int main ()
{
    long int i=1,j,n,sum,c=0;
    printf("Enter number of perfect number(s): ");
    scanf("%ld",&n);
    while(c!=n)
    {
        j=1;
        sum=0;
        while(j<i)
        {
            if(i%j==0)
                sum=sum+j;
            j++;
        }
        if(sum==i)
        {
            printf("%ld ",i);
            c++;
        }
        i++;
    }

    return 0;
}

No comments:

Post a Comment