Thread: Functions and loops assignment problem

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Functions and loops assignment problem

    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.

    Code:
    //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&#37;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++;
      }
    }
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's complaining about the lack of a return statement past the end of your while loop.

    There might be no actual way of exiting the loop by it's condition, but sometimes the compiler can't see that and issues a warning anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Hmm, i'll bare that in mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with functions and for loops
    By slava in forum C Programming
    Replies: 1
    Last Post: 11-08-2008, 01:30 PM
  2. Loops or recursive functions?
    By mariano_donati in forum C Programming
    Replies: 5
    Last Post: 05-12-2008, 12:41 PM
  3. Loops in functions?
    By AmbliKai in forum C Programming
    Replies: 16
    Last Post: 11-29-2007, 06:33 AM
  4. i dunno how to do loops and functions together
    By Noobie in forum C++ Programming
    Replies: 30
    Last Post: 02-03-2003, 06:05 PM
  5. new to Void functions and loops
    By Loraine13 in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2001, 10:00 AM