Hi

In one of my tutorials, I needed to write a program that prompts the user to enter a number, then calculate and show the prime number following the input.

The program needs to prompt the user again and again until the user enters 0.

Since its a beginner unit, no error checking is needed (so if you enter "a", the program does go crazy )

My program kind of works, but I'm not sure how to make it so that the program prompts the user for an input again if they input a negative number, since right now, I have it exit with a negative number.

Also, my lecturer once that "exit()" is evil... so is there a better way to make the program exit when the user inputs a 0?

I'm sure this isn't the best solution to the problem, so if you have any suggestions on how to improve it, it would be very much appreciated

Thanks very muchCode:/* File name: prime.c * This program takes a number, then displays the next prime number. */ #include <stdio.h> #include <math.h> #define TRUE 1 #define FALSE 0 /* Prototypes */ int isPrime(int input); int nextPrimeNumber(); int main(void){ nextPrimeNumber(); } /* Function for determining the next prime number */ int nextPrimeNumber(){ int inputNumber = 1; int nextPrimeNumber; while(inputNumber != 0){ printf("\nPlease enter an integer: "); scanf("%d", &inputNumber); if(inputNumber<=0){ printf("The number you have entered is zero or negative.\n"); exit(0); } nextPrimeNumber = inputNumber + 1; /* if the number is even, make it odd (2 is special case) */ if(nextPrimeNumber%2 == 0 && nextPrimeNumber != 2){ nextPrimeNumber+=1; } /* while its not a prime number, check the next odd number */ while(!isPrime(nextPrimeNumber)){ nextPrimeNumber+=2; } printf("The prime number following %d is %d\n", inputNumber, nextPrimeNumber); } return 0; } /* Function that checks whether or not a given number is * a prime number or not. */ int isPrime(int input){ int i; int prime = TRUE; if(input == 2){ return TRUE; } if(input%2 == 0 || input <= 1){ prime = FALSE; } else { for(i=3; i<=sqrt(input); i+=2){ if(input%i == 0){ prime = FALSE; } } } return prime; }

Ani