Tuesday, 25 February 2014

Write a ‘C’ program to check whether a given number is perfect or not. A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself. e.g., 496=1+2+4+8+16+31+62+124+248

#include<stdio.h>
main()
{   
    int num,temp,sum=0,i=1;
    printf("\nEnter a number=");
    scanf("%d",&num);
    temp=num;
    while(i<=num/2)
    {
        if(num%i==0)
            sum=sum+i;
        i++;
    }
    if(temp==sum)
        printf("\n%d is a perfect number",temp);
    else
        printf("\n%d is not a perfect number",temp);
}

No comments:

Post a Comment