Thursday, October 4, 2012

C program to display perfect,abudent,and deficiant numbers

C program to display perfect,abudent,and deficiant numbers 


what is an abundant number?
An abundant number is a number with its proper factors equalling a higher number than itself

what is a perfect number?
 A perfect number is one whose positive integer factors, other than itself, add up to the number itself. For example, the factors of 6, other than itself, are 1, 2, 3, and 1 + 2 + 3 = 6. Therefore, 6 is a perfect number.
The first eight perfect numbers are:
  • 6
  • 28
  • 496
  • 8128
  • 33550336
  • 8589869056
  • 137438691328
  • 2305843008139952128
what is a deficient number?
 A deficient number is a number with its proper factors equalling a lower number than itself.

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<math.h>
int test(int n);
void main()
{
int i,lower,upper,temp;
clrscr();
printf("Enter the range\n");
printf("\tLower Bound?");
scanf("%d",&lower);
printf("\tUpper Bound?");
scanf("%d",&upper);
if(lower>upper)
{  
temp=lower;
lower=upper;
upper=temp;
}

    printf("Number\tType\n");
    for(++lower;lower<upper;lower++)
    switch(test(lower))
          {   
           case 0:printf("%d\tperfect\n",lower);break;
           case 1:printf("%d\tAbudant\n",lower);break;
           case 2:printf("%d\tdeficiant\n",lower);break;
          }
     getch();     
}
int test(int n)
{
 int i,sum=0;
 for( i=2;i<n;++i)
   if(n%i==0)
     sum+=i;
     printf("%d",sum);
     getch();
   if(sum==n)
     return 0;
  else
    if(sum>n)
      return 1;
    else 
      return 2;
}

No comments:

Post a Comment