Tuesday, 18 March 2014

/*
 C code to find  perfect number..

 A no. said to be perfect, if sum of all its factor is equal to twice the number
e.g 6 is perfect no.
because factors are 6,3,2,1  , sum 12 ie. 2 * 6,,
while 4 is not perfect


factor of a number is perfect divisior of that number ..
i.e. at divion no remainder should left

evern no. has finite factors0 ,,

except 1, every no. has at least 2 factors , no itself and 1 ,,

*/


#include<stdio.h>
#include<conio.h>

 int perfectNo(int n);

 int main(){

 int n;
 printf("Enter no. you want to check for perfect ");
 scanf("%d",&n);
 if(perfectNo(n))
 printf(" \n\n %d is perfect number ",n);
 else
 printf(" \n\n %d is not perfect number ",n);

 return 0;}


 int perfectNo(int n){
 int ff;  //ff means factor finder
 int sumf=0; // hold sum of all the factors

 for(ff=n;ff!=0;ff--){
    if(!(n%ff))   // i.e at true factor is found else not found
    sumf+=ff;
 }

 if((n*2)==sumf)  //  to check no. is perfect or not,,
 return 1;
 return 0;
 }


No comments:

Post a Comment