One thing I can think of is that you place the algorithm in a function and you have the value as a parameter and then if the first number isnt a prime call that function with n+1 as parameter. You then simply return the prime number.
Oh and by the way, you REALLY only want to call sqrt once for every number to check, otherwise the algo will be slow.
One last thing, you should get a habit of indenting your code, let me show you a small example:
Code:
#include <iostream>
using namespace std;
int main()
{
for(int i=0; i<10; i++)
{
cout << "This is unindented code and is hard to read" << endl;
cout << "With more code it will be very hard to read if not indented" << endl;
}
cout << "So get the habit of indenting." << endl;
}
Code:
#include <iostream>
using namespace std;
int main()
{
for(int i=0; i<10; i++)
{
cout << "This is indented code and is easy to read" << endl;
cout << "You will also catch errors such as missing a closing bracket more easily." << endl;
}
cout << "So get the habit of indenting." << endl;
}
See the difference