13 Replies - 1134 Views - Last Post: 14 February 2010 - 11:05 PM Rate Topic: -----

#1 john2223322   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-February 10

prime number

Posted 13 February 2010 - 07:44 PM

I have to determine if a number is prime or not. I am just starting out with this code and have already ran into a problem: I will input a number, and then nothing happens.

#include <iostream>

using namespace std;

int main(){
    
    int input;
    
    cin >> input;
    
    while (input >= 1);{
          
          if (0 == input % 2);
             cout << input << " is not a prime number." << endl;
          if (0 != input % 2);
             cout << "something else" << endl;
          cin >> input;
}
    
    cout << input;
    
system ("pause");     

return 0;



Is This A Good Question/Topic? 0
  • +

Replies To: prime number

#2 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: prime number

Posted 13 February 2010 - 07:49 PM

If you're using a while statement, you might aswell do a "do-while" statement.

#include <iostream>

using namespace std;

int main(){
    
    int input;
    
    cin >> input;
    
    do {
          
          if (0 == input % 2);
             cout << input << " is not a prime number." << endl;
          if (0 != input % 2);
             cout << input << " is not prime." << endl;
          cin >> input;
} while (input >= 1);
    
   // cout << input;  <----- Why have the input printed twice? It's printed above. 
                       // and also, you should probably just ask the user if they
                        // wanna enter another number every time.
    
system ("pause");     

return 0;


This post has been edited by IngeniousHax: 13 February 2010 - 07:50 PM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: prime number

Posted 13 February 2010 - 08:04 PM

while (input >= 1);{

The semi-colon following the while statement -- if input is indeed >= 1 -- results in an infinite loop that does nothing except eat CPU.
Was This Post Helpful? 0
  • +
  • -

#4 Paul-   User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: prime number

Posted 13 February 2010 - 08:09 PM

Your program gets stuck in the infinite while loop. You need to remove the semicolons after the "while" and "if" statements, which in fact mean the end of the statement body.
...   
    while (input >= 1){
          
          if (0 == input % 2)
             cout << input << " is not a prime number." << endl;
          if (0 != input % 2)
             cout << "something else" << endl;
          cin >> input;
}



Was This Post Helpful? 0
  • +
  • -

#5 john2223322   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-February 10

Re: prime number

Posted 13 February 2010 - 08:17 PM

That fixed it. Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: prime number

Posted 13 February 2010 - 08:55 PM

I never knew about the syntax while(statement);. Thanks!
Was This Post Helpful? 0
  • +
  • -

#7 john2223322   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-February 10

Re: prime number

Posted 14 February 2010 - 05:42 PM

Can someone check this for me to see if it is correct. The user inputs a number and I output if it is prime or not. thanks.

#include <iostream>

using namespace std;

int main(){
    
    int input;
    
    cin >> input;
    
    
    do
    {
        if (input <= 0 || input == 1 || (input % 2 == 0 && input != 2) || 0 == input % 3 || 0 == input % 5 || 0 == input % 7 || 0 == input % 9) 
           cout << "number is not prime" << endl;
        else 
             cout << "prime number" << endl;
        cin >> input;

}
    while (input > 0);    

          

    
    
    
system ("pause");     

return 0;

}


Was This Post Helpful? 0
  • +
  • -

#8 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: prime number

Posted 14 February 2010 - 09:57 PM

Why don't you compile it and find out?
Was This Post Helpful? 0
  • +
  • -

#9 taylorc8   User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: prime number

Posted 14 February 2010 - 10:26 PM

well, the algorithm you're looking for...

--Edit--
I changed my mind, you're probably doing your homework. I'm not going to do it for you. Although I do have the working program and I accept paypal.

This post has been edited by taylorc8: 14 February 2010 - 10:35 PM

Was This Post Helpful? 0
  • +
  • -

#10 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: prime number

Posted 14 February 2010 - 10:28 PM

Don't spoonfeed.
Was This Post Helpful? 0
  • +
  • -

#11 taylorc8   User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: prime number

Posted 14 February 2010 - 10:40 PM

:D $1.00 please...
Was This Post Helpful? 0
  • +
  • -

#12 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: prime number

Posted 14 February 2010 - 10:48 PM

It doesn't work. You can use a for loop from 2 to n, breaking if you find that it evenly divides.
Was This Post Helpful? 0
  • +
  • -

#13 taylorc8   User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: prime number

Posted 14 February 2010 - 11:02 PM

My code is working. I am guessing you mean his? If you would like proof, I can supply it for $1.00
Was This Post Helpful? 0
  • +
  • -

#14 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: prime number

Posted 14 February 2010 - 11:05 PM

Of course I meant his code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1