R4R
Right Place For Right Person TM
 
R4R C C ExamplesCheck a number is perfect or not,( perfect number is sum of its factor number)
previous

Home

Next

Learn C with example

Check a number is perfect or not,( perfect number is sum of its factor number)

In this example we will explain how you can find a number is perfect or not. A perfect number is number which is sum of factor is equals to itself.

To check a number is perfect or not .We have first find its factor and add these factors and store into a variable. At last check is it equals to number itself ?If it is equals itself then it is perfect number otherwise it is not perfect number.

 

/* check a number is perfect or not,( perfect 
number is sum of its factor number) */
#include<stdio.h>
#include<conio.h>
void main()
{
int i, p,sum=0;
clrscr();
printf("Enter a number :\t");
scanf("%d", &p);
for(i=1;i<=p;i++)
{
if(p%i==0)
sum=sum+i;
}
if(sum==p)
printf("Number is perfect");
else
printf("Number is not perfect");
getch();
}

Output Of Example

Enter a number : 169
Number is not perfect
previous

Home

Next

New Updates

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R