#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    C function problem


    Hello! I'm new in c programming and the functions are already a headache. I have to make a function that returns all the prime numbers smaller than a number given. How can i return all the values in this function? Thank you.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    A function can only return one single value (in special cases the value can be a composite). You probably misinterpreted the requirements.

    I suggest you write a function that prints all prime numbers smaller than a given number.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by bdb
    A function can only return one single value (in special cases the value can be a composite). You probably misinterpreted the requirements.

    I suggest you write a function that prints all prime numbers smaller than a given number.
    How can i do that? Can you post an example?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by adi_290
    Can you post an example?
    Here's a complete program that prints all numbers less than 1000 that can be written as a product of 3 consecutive integers

    Code:
    #include <stdio.h>
    
    int isvalid(int n) {
        int k = 1;
        int product;
        do {
            product = k * (k + 1) * (k + 2);
            k++;
        } while (product < n);
        return (product == n);
    }
    
    void printall(int n) {
        int k;
        printf("Numbers:");
        for (k = 1; k < n; k++) {
            if (isvalid(k)) printf(" %d", k);
        }
        printf("\n");
    }
    
    int main(void) {
        printall(1000);
        return 0;
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by bdb
    Here's a complete program that prints all numbers less than 1000 that can be written as a product of 3 consecutive integers

    Code:
    #include <stdio.h>
    
    int isvalid(int n) {
        int k = 1;
        int product;
        do {
            product = k * (k + 1) * (k + 2);
            k++;
        } while (product < n);
        return (product == n);
    }
    
    void printall(int n) {
        int k;
        printf("Numbers:");
        for (k = 1; k < n; k++) {
            if (isvalid(k)) printf(" %d", k);
        }
        printf("\n");
    }
    
    int main(void) {
        printall(1000);
        return 0;
    }
    Got it! Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo