‘C’ program to check if a number is perfect

C File =>


/* Write a ‘C’ program to check if a number is perfect (number = sum of its factors) */

#include <stdio.h>
int main() 
{
     int n, i,sum=0;

     printf("\n Enter a positive integer: ");
     scanf("%d",&n);

     printf("\n Factors of %d are: ", n);
     for (i = 1; i<n; ++i) 
    {
        if (n % i == 0) 
       {
              printf("%d\t", i);
              sum=sum+i;
         }
     } //end for

     if(sum==n)
          printf("\n %d is perfect number",n);
     else
          printf("\n %d is Not a perfect number",n);

     return 0;
}

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.