954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Trouble with Prime Number Program

I am writing a program that will take input and find whether or not the number you put in is prime. The program works fine, save for one small problem:

#include <iostream>
using namespace std;
int main(){
    int num; //the chosen number
    int i;   //potential factors
    
    do{
    cout<<"Input a positive integer: \n";
    cin>>num;
    cin.ignore();
    
//these if statements take care of all "abnormal" situations:
    if (num == 1){
       cout<<"1 is composite, not prime. Try again.\n";
       }
    if (num < 1){
       cout <<"Not valid input. Try again.\n";
       }
    if (num == 2){
       cout <<"2 is prime.\n";
       cin.get();
       }
} while (num < 2);

//factor iteration:
    for (i = 2; i < num; i++){
        if (num % i == 0){
            cout<< num <<" is not prime. It is divisible by " << i << "\n";
            cin.get();
            }
        if (num % i != 0){
                cout<< num <<" is prime!\n";
                cin.get();
                }
        else {
             cout<<"An error occured.\n";
             cin.get();
             }
        }
}


The problem is in the if statements embedded in the for loop that iterates through the possible factors of "int num". As you may have guessed, I have to press enter as many times as the input minus one. So, for every time the "i" variable increments. I tried to use a break statement for each one, but this declares every odd number as prime, like 15 or 39. Any help as to how to fix this problem would be greatly appreciated.

On a side note, just for future reference on DaniWeb, do I have to include all the code in the code tags, or just the part that has the problem? If it's a really long program, then of course it would be stupid to include all of it...Also, is there a way to enable syntax highlighting? Anyway, thanks.

orwell84
Junior Poster
102 posts since Nov 2008
Reputation Points: 17
Solved Threads: 5
 

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
 

I had something before where it was:

for ((i = 2; i < num; i++){
        if (num % i == 0){
               primecheck = 1;
               }
        if (num %i != 0){
               primecheck = 0;
               }


Then after that, I had a bunch of if statements that determined what to display based on the value of int primecheck. For some reason, however, it always displayed the error message when I input any number except 2 or anything below that.

orwell84
Junior Poster
102 posts since Nov 2008
Reputation Points: 17
Solved Threads: 5
 

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
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You