Primality program

This is what I need help with:
Prompt the user for an integer then output "prime" if the number is prime or "composite" is the number is composite. Then output the next higher prime number. You must write a function with the following signature: int is_prime(int value). This function takes a number and returns 1 if the given number is prime and 0 if it is composite.

so far I got this:

#include <iostream>
#include <cmath>
using namespace std;

int is_prime(int value)
{
if (value == 1)
{
cout << "1 is not prime!\n";
cout << "The next prime number after 1 is 2\n";
return 0;
}
if (value == 2)
{
cout << "2 is prime!\n";
cout << "The next prime number after 2 is 3\n";
return 1;
}

for(int i = 2; i <= sqrt(value); i++)
{
if (value % i == 0)
cout << value << " is not prime!\n";
cout << "The next prime number after " << value;
}
}

int main()
{
int value;

cout << "== Checking for Primality ==\n";
cout << "Enter an integer:\n";
cin >> value;
is_prime(value);
}

I dont know whether I'm on the right path or not. I've tried other things and they've worked but my only problem is trying to find the formula to check for the next prime number. Thank you for any help.
You'll just have to check them all
1
2
3
4
5
6
7
8
bool prime;
for(int a = value+1; ; a++){
   prime = 1;
   for(int i = 2; i*i <= value; i++){
      if (value % i == 0) prime = 0;
   }
   if(prime) break;
}
oh ok i think i know what you mean thank you for the help and i'll let you know if i have any issues
alright there is one part that i'm having trouble with. what part of that code is that value for the next prime number? when write it into my code i always get that the next prime number is 1.
Oh wait. You're right. My code is wrong.
1
2
3
4
5
6
7
8
9
10
bool prime;
int next;
for(next = value+1; ; next++){
   prime = true;
   for(int i = 2; i*i <= next; i++){
      if (next % i == 0) prime = false;
   }
   if(prime) break;
}
cout << next;
wow thank you so much! that worked perfectly!
Topic archived. No new replies allowed.