Writing a program in C that uses recursion to determine if a number is prime or not. It works until you try it with a prime number above 9431. Anything higher than that gets a stack overflow error. I was wondering if there was some way to fix this.
I haven't really tried anything other than see at which number it fails at, which varies each time.
//Remove scanf error
#define _CRT_SECURE_NO_WARNINGS
//Preprocessor directives
#include<stdio.h>
#include<stdlib.h>
//Recursion function
int PrimeCheck(int choice, int i)
{
    //Check if integer i is reduced to 1
    if (i == 1)
    {
        return 0;
    }
    else
    {
        //Check to see if number choice is divisible by value i
        if (choice % i == 0)
        {
            return 1;
        }
        //Call the function again but reduce the second variable by 1
        else
        {
            return PrimeCheck(choice, i - 1);
        }
    }
}//End PrimeCheck function
//Main function
main()
{
    //Assign needed variables
    int choice, num;
    //ask for user input
    printf("Please enter a number between 2 and %i:", INT_MAX);
    scanf("%i", &choice);
    //Check for numbers outside the range
    if (choice < 2 || choice > INT_MAX)
    {
        printf("Please try again and enter a valid number.\n");
        system("pause");
        return 0;
    }
    //Call the PrimeCheck "looping" function
    num = PrimeCheck(choice, choice / 2);
    //Display result for the user
    if (num == 0)
    {
        printf("%i is a prime number.\n", choice);
    }
    else
    {
        printf("%i is NOT a prime number.\n", choice);
    }
    system("pause");
}//End main
The output should be "____ is a prime number" or "____ is NOT a prime number" The actual output above 9431 is a stack overflow error.
 
    