Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,060 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,393 people online right now. Registration is fast and FREE... Join Now!




Prime number

2 Pages V  1 2 >  
Reply to this topicStart new topic

Prime number, C++

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

D.I.C Head
**

Joined: 28 Sep, 2005
Posts: 55



Thanked: 2 times
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
+Quote Post


Dark_Nexus
RE: Prime Number
9 Nov, 2005 - 12:21 AM
Post #2

or something bad...real bad.
Group Icon

Joined: 2 May, 2004
Posts: 1,318



Thanked: 6 times
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 offlineProfile CardPM
+Quote Post

Kcbroncofan
RE: Prime Number
9 Nov, 2005 - 04:51 AM
Post #3

D.I.C Head
**

Joined: 28 Sep, 2005
Posts: 55



Thanked: 2 times
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
+Quote Post

Amadeus
RE: Prime Number
9 Nov, 2005 - 06:04 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,976



Thanked: 116 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
+Quote Post

bullet proof penguin
RE: Prime Number
15 Nov, 2005 - 12: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
+Quote Post

bullet proof penguin
RE: Prime Number
16 Nov, 2005 - 12: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
+Quote Post

mikepetro
RE: Prime Number
18 Nov, 2005 - 11:28 PM
Post #7

New D.I.C Head
*

Joined: 15 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: 18 Nov, 2005 - 11:32 PM
User is offlineProfile CardPM
+Quote Post

sonal.12
RE: Prime Number
14 Sep, 2008 - 08:40 PM
Post #8

New D.I.C Head
*

Joined: 14 Sep, 2008
Posts: 2

QUOTE(Kcbroncofan @ 8 Nov, 2005 - 10:01 PM) *

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.

ya i know but i hav 2 find out using functions
User is offlineProfile CardPM
+Quote Post

homemade-jam
RE: Prime Number
15 Sep, 2008 - 02:37 AM
Post #9

eee Guru
Group Icon

Joined: 17 Mar, 2008
Posts: 1,243



Thanked: 4 times
Dream Kudos: 50
My Contributions
You only have to divide by a number upto the square root of the number, as after this you are just doing the sums the opposite way around
User is offlineProfile CardPM
+Quote Post

numerical_jerome
RE: Prime Number
15 Sep, 2008 - 07:20 PM
Post #10

D.I.C Head
**

Joined: 16 Sep, 2007
Posts: 164



Thanked: 10 times
My Contributions
also a hint for performance improvement, with the exception of 2 & 3, all primes are of the form (6n)+1 or (6n)-1, where n is a positive integer.* using this as a first test can greatly reduce the run time of your isprime(int) function, and only costs two statements:

CODE

int isprime(int input)
{
    if(input % 6 != 1 || input % 6 != 5)
    {
        return(0);
    }
}



* if you submit a proof of this with your assignment, your professor may give you extra points, or not, but still cool
User is offlineProfile CardPM
+Quote Post

whelpy
RE: Prime Number
5 Jun, 2009 - 01:28 AM
Post #11

New D.I.C Head
*

Joined: 5 Jun, 2009
Posts: 1

Hey Dude, how about this? It's not 'that' lengthy and I find it working perfectly well. Just apply rudimental arithmatics (the use of modulus for example) and you will easily finish it. Hope it helps:

CODE

#include <stdio.h>
#include <stdlib.h>
#define PRIME 1
#define NOT_PRIME 0
int main(int argc, char *argv[])
{
  int num,flag,i,count=0;
  printf("Enter number : ");        
  scanf("%d",&num);
  flag=PRIME;
  for(i=2;i<num;i++)
  {
   if(num%i==0)
   flag=NOT_PRIME;
  }
  if(flag==PRIME)
    printf("\n%d is PRIME\n",num);
  else
    printf("\n%d is NOT PRIME\n",num);  
  system("PAUSE");    
  return 0;
}


Mod edit: added code tags: code.gif
User is offlineProfile CardPM
+Quote Post

Kanvus
RE: Prime Number
5 Jun, 2009 - 01:37 AM
Post #12

D.I.C Regular
Group Icon

Joined: 19 Feb, 2009
Posts: 321



Thanked: 26 times
Dream Kudos: 50
My Contributions
You guys are silly. Find a chart of prime numbers. Write an if for each number. If it matches one of them, say yay and if matching none, say nay. And if they input a number higher than all the ones you wrote, ask them to try again. Trust me. It's the truth.
User is offlineProfile CardPM
+Quote Post

janotte
RE: Prime Number
5 Jun, 2009 - 02:03 AM
Post #13

code > sword
Group Icon

Joined: 28 Sep, 2006
Posts: 2,137



Thanked: 150 times
Expert In: C/C++

My Contributions

Try following posting rule 7
Search for your problem before posting a new topic
and you will save yourself a lot of time and heartache (and having to read useless suggestions like the one from Kanvus)


Have a look at the top result from using the little search box up there in the top right.
http://www.dreamincode.net/code/snippet2196.htm
Useful isn't it!



User is offlineProfile CardPM
+Quote Post

Kanvus
RE: Prime Number
5 Jun, 2009 - 02:12 AM
Post #14

D.I.C Regular
Group Icon

Joined: 19 Feb, 2009
Posts: 321



Thanked: 26 times
Dream Kudos: 50
My Contributions
^u get a trophy for being most serious 2009
User is offlineProfile CardPM
+Quote Post

computerfox
RE: Prime Number
5 Jun, 2009 - 01:12 PM
Post #15

JAVA Physicist
Group Icon

Joined: 29 Jan, 2009
Posts: 3,331



Thanked: 38 times
Dream Kudos: 1300
My Contributions
hope this helps
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Prime Number
5 Jun, 2009 - 01:34 PM
Post #16

Alphanumeric
Group Icon

Joined: 18 Feb, 2007
Posts: 4,294



Thanked: 189 times
Dream Kudos: 1050
Expert In: Java/C++

My Contributions
QUOTE(whelpy @ 5 Jun, 2009 - 04:28 AM) *

Hey Dude, how about this? It's not 'that' lengthy and I find it working perfectly well. Just apply rudimental arithmatics (the use of modulus for example) and you will easily finish it. Hope it helps:

CODE

#include <stdio.h>
#include <stdlib.h>
#define PRIME 1
#define NOT_PRIME 0
int main(int argc, char *argv[])
{
  int num,flag,i,count=0;
  printf("Enter number : ");        
  scanf("%d",&num);
  flag=PRIME;
  for(i=2;i<num;i++)
  {
   if(num%i==0)
   flag=NOT_PRIME;
  }
  if(flag==PRIME)
    printf("\n%d is PRIME\n",num);
  else
    printf("\n%d is NOT PRIME\n",num);  
  system("PAUSE");    
  return 0;
}


Mod edit: added code tags: code.gif


People sure like this method... its very common and I called it the "naive" Prime finder because it is the most common solution. One little improvement over this method is just to limit the search from 2 to sqrt(num) rather than num... the reason for this is that if a factor is greater than sqrt(num) then its pair factor must be smaller (else when you multiplied them the result would be greater than num).

But we can even do better than limiting our factor search to just the sqrt(num)... if we rule out 2 then we only have to check out odd numbers. This makes things even faster.

We can even do better than that since all we really need to check are the prime numbers between 2 and sqrt(num) which is generally FAR less than sqrt(num)/2 comparisons.

User is offlineProfile CardPM
+Quote Post

MajorWalrus
RE: Prime Number
5 Jun, 2009 - 01:46 PM
Post #17

New D.I.C Head
*

Joined: 22 Apr, 2009
Posts: 25



Thanked: 5 times
My Contributions
QUOTE
* if you submit a proof of this with your assignment, your professor may give you extra points, or not, but still cool


Is there a proof for this? I can look it up myself, but I'm wondering if this particular prime technique has a name.

Thanks!
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Prime Number
5 Jun, 2009 - 02:20 PM
Post #18

Alphanumeric
Group Icon

Joined: 18 Feb, 2007
Posts: 4,294



Thanked: 189 times
Dream Kudos: 1050
Expert In: Java/C++

My Contributions
The proof is pretty strait forward as it is the definition of a prime... "A number only divisible by 1 and itself" -- so if it is not divisible by any numbers between 1 and itself -- it is prime -- if it Is divisible by a number in that range then it is not prime.

Might want to explain how modulus determines divisibility -- but that is it.

Other than something like "The naive algorithm" I don't think this really has a name because it really is just the definition.

see Primality Test
User is offlineProfile CardPM
+Quote Post

fastman007
RE: Prime Number
11 Jun, 2009 - 11:16 PM
Post #19

New D.I.C Head
*

Joined: 12 Apr, 2009
Posts: 25



Thanked: 4 times
My Contributions
CODE

#include <iostream.h>
#include <conio.h>
void prime(int);
main()
{
int no;
cout << "Enter a no. ";
cin>>no;
    if (no < 0)
     {
        cout <<"pls.. enter a positive no.";
     }
     else
     {
prime(no);
     }
}

void prime(int i)
{

    if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0 || i % 7 == 0  )
    {
            cout<<i<<" is not s prime no."<<endl;
    }


    else cout<<i<<" is a prime number";
}


User is offlineProfile CardPM
+Quote Post

Kanvus
RE: Prime Number
11 Jun, 2009 - 11:43 PM
Post #20

D.I.C Regular
Group Icon

Joined: 19 Feb, 2009
Posts: 321



Thanked: 26 times
Dream Kudos: 50
My Contributions
^ omg ban

no jk. wanna make a club?
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic

Time is now: 7/3/09 11:17PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month