Thursday, 2 December 2010

write a function test a number is perfect number or not?

//write a function test a number is perfect number or not:
//perfect number means sum of the devisors must be equal to given number
int perfect(int n)
{
 int i,sum=0;
 for(i=1;i<=n/2;i++)
 {
  if(n%i==0)

   sum+=i;
  }
  return(n==sum);
 }
 void main()
 {
  int n,perfect(int);
  printf("enter any value\n");
  scanf("%d",&n);
  if(perfect(n))
   printf("perfect number\n");
  else
   printf("not a perfect numer\n");
  getch();
 }

No comments:

Post a Comment