# Thread: Calculating next prime number

1. ## Calculating next prime number

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

Code:
```/* 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 main(void){

}

/* Function for determining the next prime number */

int inputNumber = 1;

while(inputNumber != 0){
scanf("%d", &inputNumber);

if(inputNumber<=0){
printf("The number you have entered is zero or negative.\n");
exit(0);
}

/* if the number is even, make it odd (2 is special case) */
}

/* while its not a prime number, check the next odd number */
}

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;
}```
Thanks very much

Ani

2. Code:
```if(input == 0)
{
break; // to interrupt the while-loop
}

if(input < 0)
{
cout << "Wrong input!";
cin.ignore(1000, '\n');
cout << "Press enter to try again!";
cin.get();
continue; // start while-loop from beginning.
}```

3. Originally Posted by Ideswa
Code:
```if(input == 0)
{
break; // to interrupt the while-loop
}

if(input < 0)
{
cout << "Wrong input!";
cin.ignore(1000, '\n');
cout << "Press enter to try again!";
cin.get();
continue; // start while-loop from beginning.
}```
C++ on C board

ssharish2005

4. oops!!

5. 1) In the function isPrime. once you find that the number is not
prime, there is no reson to keep the for loop.
2) I don't know why you where told that the exit function is
3) If you want the user to keep input even with negative
numbers, why not let the while loop do it ?
Code:
`    while(inputNumber != 0)`

Code:
```#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <string.h>
#include <time.h>
#include <assert.h>

#define TRUE 1
#define FALSE 0

/* Prototypes */
int isPrime(int input);

int main(void)
{

printf("Press enter to end ....\n");
getchar();
return(0);
}

/* Function for determining the next prime number */
{
int inputNumber = 1;

while(inputNumber != 0)
{
printf("\nPlease enter an integer [0 - exit]: ");
scanf("%d", &inputNumber);

if ( inputNumber < 0 )
printf("The number you have entered is negative. Try again\n");
else if ( inputNumber > 0 )
{

/* if the number is even, make it odd (2 is special case) */

/* while its not a prime number, check the next odd number */

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) && prime == TRUE  ; i+=2)
if(input%i == 0)
prime = FALSE;
}
return prime;
}```

why is the function nextPrimeNumber return a integer ?
You dont the check the returned value in main anyway.

6. :P I'm new to C, so I'm not sure what the normal return types should be.

But after I went through your code a bit, I get it now

Thanks very much for your time
Ani

7. A function may return any kind of data type.
But it may return no value at all. In this case the function
is prototyped as returing "void", but you may use the
return without a value.

The next code is an exapmle.

Code:
```/* the prototype : */
void sum(int a, int b);

int main(void)
{
int result;

/* The compiler will warn you, on the next line :
result = sum(3,5);
*/
/* sum returns with no value */
sum(3, 5);
}

void sum(int a, int b)
{
if ( a < 0 || b < 0 )
return; /* return with no value */
printf("a+b = %d\n", a+b);
}```

8. and main should return a value as well
Code:
```int main(void)
{
int result;

/* The compiler will warn you, on the next line :
result = sum(3,5);
*/
/* sum returns with no value */
sum(3, 5);
return 0;
}```
ssharish2005

9. Oh ok, so if a function does not return anything with "return", then it should start with "void".

Thanks
Ani