Pages

Ads 468x60px

Saturday, 2 June 2012

C Program For PERFECT NUMBER


C Program For PERFECT NUMBER



Here is a program to find whether the entered number is a perfect or not. A perfect number is a one, whose sum of devisors is equals the number itself. Logic: Here in the program, user need to enter the number to check if it is perfect.  The for loop in the program traces the iteration till the number, while in each of  iteration it checks the present number is the divisor of the entered or not. If it is, it adds it to the flag, which holds the sum of all the divisors. Finally, it checks if  the sum equals the given number.





CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int c=0,n,i;
clrscr();<br>
printf("Enter n value:");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
{
c=c+i;
}
}
if(n==c)
{
printf("given number %d is perfect number",c);
}
else
{
printf("not perfect number");
}
getch();
}

0 comments:

Post a Comment

Total Pageviews