I have to write a function which returns 1 if n is a perfect number and 0 otherwise. A perfect number is that which when all it's factors besides it's self is added up equals itself. For e.g. the factors of 6 are 1, 2, 3 and 6. 1+2+3 = 6, therefore it's a perfect number.
I've rewritten the code several times, and I split one task to the main function and the other to the isPerfect function, I get a control reaches non void error, and I can't figure out what's wrong with the function, the curly brackets seem to be ok, but they obviously are not.
ThanksCode://Write a function isPerfect(int n) which returns 1 if n is a perfect number and 0 otherwise. #include <stdio.h> #define PERFECT 1 #define NOT_PERFECT 0 int isPerfect (int n); int main (int argc, char **argv) { int n, i;//n is number being tested n = 1; while (n <= 10000){ i = isPerfect ( n ); printf ("\n%d is a perfect number", n); n++; } return (0); } int isPerfect(int n) { int k = 1;//factor numbers to be tested int sum = 0;//summing up the factors int test;//testing if the final summation is equal to the number being tested for perfectness while (k <= 10000){ if (n % k == 0) { sum = sum + k; } else { test = (sum - n); } if (test == n){ return (PERFECT); } else { return (NOT_PERFECT); } k++; } }



LinkBack URL
About LinkBacks


