Recursive function to check if all digits in an int variable are even
c, recursion
Solution
You should write it down this way to compile the code:
void allEven(int number, int *result)
{
if ((number % 10) % 2) // if the last digit is odd
{
*result = 0;
}
else
{
*result = 1;
if ((number / 10) != 0) //not the last digit to evaluate, we call the function again.
{
allEven((number / 10), result);
}
}
}
int main()
{
int num;
int result;
printf("Enter a number: ");
scanf("%d", &num);
allEven(num, &result);
printf("allEven(): %d", result);
}
1) "int* result" replace with "int result"
2) "allEven((number/10), &result)" call in main() replace with allEven((number/10), result)
3) you missed a brace in allEven function
Problem
I am trying to write a recursive function to check whether a user input a number which contains all even digits. What is wrong with my logic? When I tried with "556" result is 1. ``` int main() { int num; int *result; printf("Enter a number: "); scanf("%d", &num); allEven(num, &result); printf("allEven(): %d", result); } void allEven(int number, int *result) { if ((number % 10) % 2) // if the last digit is odd { *result = 0; } else { *result = 1; if ((number / 10) != 0) //not the last digit to evaluate, we call the function again. { allEven((number / 10), &result); } } } ```