I'm trying to write a programme with C that asks for a number to be input and then determines whether or not it is a prime number. If the input is a non-numeric character or string, it outputs the message INVALID INPUT. I have managed to create code that can determine if a number is prime and also if it is non-numeric but when I try and combine them, it doesn't work. E.g if I input a non-numeric value it will output the correct message but when I input a numeric value, nothing happens and I'm not sure why. Can anybody help?
#include <stdio.h>
#include <string.h>
#define FALSE 0
#define TRUE 1
int numeric( char *string )
{
int i, valid;
valid = TRUE;
if (string[i] < '0' || string[i] >'9')
valid = FALSE;
return valid;
}
void main()
{
char number[5];
printf("Please enter a decimal number>");
gets( number );
if (numeric(number) == TRUE)
{
int n, i, a = 0;
scanf_s("%d", &n);
for (i = 2; i <= n / 2; ++i)
{
if (n%i == 0)
{
a = 1;
break;
}
}
if (n == 1)
{
printf("1 isn't a prime number.");
}
else
{
if (a == 0)
printf("number entered is a prime number");
else
printf("number entered is not a prime number");
}
}
else
printf("INVALID INPUT \n");
}