I am trying to answer this problem:
Write a function that is given an integer, n, by the main method where 1 ≤ n ≤ 9999 and prints whether it is even, odd, or/and prime. Write your answer in the form:
2 is even and prime
The code I have written so far is:
/*Lab2 ex4 */ #include <stdio.h> #include <math.h> #include <stdbool.h> bool IsPrime(int num) { int i; for (i=2; i<num; i++) { if (num%i == 0) { return false; } } return true; } bool IsEven(int num) { if (num%2 == 0) { return true; } else { return false; } } char BoolToString(bool prime, bool even) { if (prime == true, even == true) { char* result = "is even and prime"; return result; } else if (prime == true, even == false) { char* result = "is odd and prime"; return result; } else if (prime == false, even == true) { char* result = "is even and not prime"; return result; } else if (prime == false, even == false) { char* result = "is odd and prime"; return result; } else { char* result = "error"; return result; } } main() { printf("%d %c\n", 11, BoolToString(IsPrime(11), IsEven(11))); } But I get the error message return makes integer from pointer without a cast on the return statements in the BoolToString function.
I don't understand what I've done wrong here? Many thanks.
(I only started C about 2 weeks ago so apologies if I've done something in a horrible way or completely misunderstood how something is used.)
char *BoolToString(). If you want to combine two booleans use&&, and use the bool values directly:if (prime == false, even == true)should beif (!prime && even). ("If not prime and even".) The comma in that case is an operator which returns the value of the second expression, so that the first test is completely ignored. Interesting mistake which does not generate an error or even warning, does it?