Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 109,492 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,180 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Prime number

 
Reply to this topicStart new topic

Prime number, C++

Kcbroncofan
post 8 Nov, 2005 - 10:01 PM
Post #1


D.I.C Head

**
Joined: 28 Sep, 2005
Posts: 55


My Contributions


I need to write a program that asks a user to input a positive integer. I then need to out put whether the number is prime. I also need to put in an error message if the user inputs a negative number.
User is offlineProfile CardPM

Go to the top of the page


Dark_Nexus
post 9 Nov, 2005 - 01:21 AM
Post #2


or something bad...real bad.

Group Icon
Joined: 2 May, 2004
Posts: 1,308



Dream Kudos: 625
My Contributions


Could you please post what you have attempted so far, and we will be more than happy to guide you in the right direction.
User is online!Profile CardPM

Go to the top of the page

Kcbroncofan
post 9 Nov, 2005 - 05:51 AM
Post #3


D.I.C Head

**
Joined: 28 Sep, 2005
Posts: 55


My Contributions


This is what I have.

#include <iostream>

using namespace std;

int isprime(int Prime_Number);
int main()
{
int Number = 0;

int isprime(int Prime_Number)
{
for(int Temp = 2;Temp < Prime_Number;Temp ++)
if(!(Prime_Number % Temp))
return(0); return(1);
}

}
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 9 Nov, 2005 - 07:04 AM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 11,781



Thanked 17 times

Dream Kudos: 25
My Contributions


Well, you are declaring the function inisde the main function...that is a no no, I'm afraid...you should declare like so:
CODE

#include <iostream>

using namespace std;

int isprime(int Prime_Number);
int main()
{
int Number = 0;
cout<<"Enter a number"<<endl;
cin>>Number;
if(Number>0)
{
  if(isprime(Number)==0)
  {
     cout<<"Number is prime"<<endl;
  }
  else
  {
     cout<<"Number is not prime"<<endl;
  }
}
else
{
  cout<<"Positive numbers only"<<endl;
}
  return 0;
}


int isprime(int Prime_Number)
{
//prime number calculation here
}

As for finding a prime number, simply start at 1, loop to the number itslef and use the modulus...if the result of that operation (userNumber%loop number) equals 0, and the loop number is something other than 1 or the user number, the number is not prime.
User is offlineProfile CardPM

Go to the top of the page

bullet proof penguin
post 15 Nov, 2005 - 01:43 PM
Post #5


New D.I.C Head

*
Joined: 15 Nov, 2005
Posts: 5


My Contributions


just an idea, limited

have a do while trying to mod divide the input int by 2 and up (++) until you hit the input value and if anything else returns 0 than the number is not prime. about the error if neg just check if the input value is < 0.
User is offlineProfile CardPM

Go to the top of the page

bullet proof penguin
post 16 Nov, 2005 - 01:19 PM
Post #6


New D.I.C Head

*
Joined: 15 Nov, 2005
Posts: 5


My Contributions


so i also needed to do this for a class and this is my code, seems to work just fine.

QUOTE

/*
input positive integer and determine if it is prime
*/

#include <iostream.h>

void main()
{
long xaero;
int loop=42;
do
{
  long x=2;
  int p=0;
  cout << "\n\nPlease enter a positive non zero number (-13 to quit)\n";
  cin >> xaero;

  if (xaero == -13)
  {
  loop=13;
  }

  else
  {
  if (xaero < 1)
  {
    cout << "I said a POSITIVE NON ZERO";
    p=13;
  }
  else
  {
    for (x;x<=xaero;x++)
    {
    if ((xaero%x)==0 && (xaero != x))
      {
      x=xaero;
      cout << xaero << " is not a prime number";
      p=1;
      }
    }
  }
  if (p==0)
  {
    cout << xaero << " is a prime number";
  }
  }
}while (loop != 13);
}
User is offlineProfile CardPM

Go to the top of the page

mikepetro
post 19 Nov, 2005 - 12:28 AM
Post #7


New D.I.C Head

*
Joined: 16 Nov, 2005
Posts: 8


My Contributions


Hey man,

I know I'm going to get thrashed for this... but it happens. Here is my code written in JAVA for the same exact problem. I have it done in JAVA and the theory is the same, all you have to do is change the syntax. So here it is:

CODE
for (int i = 2; i <= (Math.sqrt(primeInput)); i++)
    {
 if ((primeInput % i) == 0)
     {
   isPrime = false;
   break;
     }
    }
    
    if (isPrime)
 JOptionPane.showMessageDialog(null, "The number you entered " + primeInput + " is PRIME!!", "We have found a prime!", JOptionPane.INFORMATION_MESSAGE);  
    else
 JOptionPane.showMessageDialog(null, "The number you entered " + primeInput + " is NOT PRIME!!", "We have FAILED!", JOptionPane.ERROR_MESSAGE);
    



Also, I forgot about your non-negative number input, once again here is my code in JAVA.

CODE
while (primeInput < 0)
    {
    JOptionPane.showMessageDialog(null,"That was not a positive integer!", "That was not a positive integer!", JOptionPane.ERROR_MESSAGE);
 
    inputStr = JOptionPane.showInputDialog("Please input a positive integer:  ");
    primeInput = Integer.parseInt(inputStr);
    }


This post has been edited by mikepetro: 19 Nov, 2005 - 12:32 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 9/7/08 02:16PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month