Thursday 24 January 2013

Perfect Number

////Program to check if a given no. is perfect no.  or not

#include <stdio.h>

/*Function to find the sum of divisors of the number n*/
int sum_of_divisors(n)
{
    int i , sum = 0 ;
   
    for (i=1;i<=n-1;i++)
           if (( n%i )==0)
        sum=sum+i;
    return sum;
}

/*Function to check if the given number is perfect or not by checking the sum of divisors*/
int check_perfect(n)
{
    int sum ;
   
    sum = sum_of_divisors(n);
   
    if (sum==n)
        return 1;
   
    else
        return 0;
}

/*main function*/
int main()
{
    int i , n , check;
   
    printf("enter the no.");
    scanf("%d",&n);

    check = check_perfect(n);
    if (check == 1)
        printf("given no. is perfect no.\n");

    else
        printf ("given no. is not a perfect \n");
}

No comments:

Post a Comment