Completely Solved C, C++ Programs Assignment.




Write a c program to check given number is perfect number or not.

Filed Under:

/*Write a c program to check given number is perfect number or not.*/
#include <stdio.h>
#include <conio.h>
int main(){
  int n,i=1,sum=0;
  printf("\nEnter a number:-");
  scanf("%d",&n);
  while(i<n);
{
      if(n%i==0)
           sum=sum+i;
          i++;
  }
  if(sum==n)
      printf("\nThe no %d is a perfect number",i);
  else
      printf("\nThe no %d is not a perfect number",i);
  return 0;
}
/*   source:http://mycomputerscience.net    */


Definition of perfect number:

Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3.  Sum of its divisor is
1 + 2+ 3 =6

Note: 6 is the smallest perfect number.

Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128


Get Free Programming Tutorials and Solved assignments

0 comments:

Post a Comment