Search code examples
cfunctionnumberscallfunction-call

How to successfully output a function call?


My assignment is to check if a number is prime, but I have to use three sections to do it. The first is the main body of code and that is followed by two functions. The first checks if the number is even, and the second checks if it is prime. I know this is a rather tedious way to check if a number is prime but it is meant to get us introduced to functions and function calls!

UPDATE

I have gotten it all to work besides printing the smallest divisor of a non prime number. I thought using i from the second function would work but it will not output. I have copied by code below -- please help if you have any suggestions!

#include <stdio.h>
#include <math.h>
int even (int);
int find_div (int);
int main() {
    int num, resultEven, resultPrime, i;

    printf("Enter a number that you think is a prime number (between 2 and 1000)> \n");
    scanf("%d", &num);

    while (num < 2 || num > 1000) {
        if (num < 2) {
            printf("Error: number too small. The smallest prime is 2.\n");
            printf("Please reenter the number > \n");
            scanf("%d", &num);
    }
        else if (num > 1000) {
            printf("Error: largest number accepted is 1000.\n");
            printf("Please reenter the number > \n");
            scanf("%d", &num);
    }
        else {
    }
    }

        resultEven = even(num);

    resultPrime = find_div(num);

        if (resultEven == 1) {
            printf("2 is the smallest divisor of %d. Number not prime\n", num);
        }
        else if (resultPrime == 1) {
            printf("%d is the smallest divisor of %d. Number not prime\n", i, num);
        }
        else {
            printf("%d is a prime number.\n", num);
        }


    return 0;
}
int even(int num) {
    if (num % 2 == 0) {
        return 1;
    }
    else {
        return 0;
    }
}
int find_div(int num) {
    int i;

    for (i = 2; i <= (num/2); i++) {
        if (num % i == 0) {
            return 1;
        }
        if (num == i) {
            return 0;
        }
    }
    return i;
}

Solution

  • I would create a function for Wilsons theorem (p-1)! = 1 (mod p) iff p is prime, first off, to make the functions nice and easy you will only need the one. for numbers less than 1000 it should work fine.

    something like,

    //it will return 1 iff p is prime
    int wilson(int p)
    {
        int i, result = 1;
        for (i = 0; i < p; i++)
        { 
             result *= i;
             result = result % p;
        }
    
        return result;
    }
    

    however if your not printing check that you have included, at the top of your file

     #include <stdio.h>
    

    your

    resultEven = even(num) 
    

    needs a ; at the end but that was mentioned in the comments, besides that your methodology though odd is correct, also you do not need the empy else, that can simply be removed and your good

    UPDATE:

    //if return value == 1 its prime, else not prime, and 
    //return value = smallest divisor
    int findDiv(int p)
    {
        int i= 0;
        for (; i <= n/2; i++)
        {
            //you number is a multiple of i
            if (num % i == 0)
            {
                //this is your divisor
                return num;
            }
        }
        //1 is the largest divisor besides p itself/smallest/only other 
        return 1;
    }