Get rid of all the cin.get( ) statements.
Your loop will give a multitude out outputs for any test value. You need logic that will halt when input number is shown to be non-prime, or let the loop go the whole way, keeping track if non-primeness has been detected.
At the very least, you need to use if...else if structure - your final else that error has occurred fires when it shouldn't.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
Look at your for loop. Mathematics says that you just need to check it till i<=sqrt(n) i.e. the square root of n. That is, it your input number is 33, then just check that if should not be divisible by any number from 2 to sqrt(33) which is 5. So you see, that is just 3 iteration rather than 31 iterations what you were doing.
Learn about break; statement.
Don't announce just yet that the number is prime repeatedly. Use concept of flags. That is to say, define a variableflag and initialize it to 1. Now run your loop and test the condition (n%i==0) . If you find this condition to be true, just say flag=0; and issue a break;
Now, when you are out of loop, flag will have value 1 if the number is prime, else it will be set to zero.
This method thus will be more efficient.
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140