Prime number

C++

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 31657 Views - Last Post: 11 January 2010 - 06:25 AM Rate Topic: -----

#1 Kcbroncofan   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 55
  • Joined: 28-September 05

Prime number

Posted 08 November 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.
Is This A Good Question/Topic? 1

Replies To: Prime number

#2 dorknexus   User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1272
  • View blog
  • Posts: 4,625
  • Joined: 02-May 04

Re: Prime number

Posted 09 November 2005 - 01:21 AM

Could you please post what you have attempted so far, and we will be more than happy to guide you in the right direction.
Was This Post Helpful? 0
  • +
  • -

#3 Kcbroncofan   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 55
  • Joined: 28-September 05

Re: Prime number

Posted 09 November 2005 - 05:51 AM

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);
}

}
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Prime number

Posted 09 November 2005 - 07:04 AM

Well, you are declaring the function inisde the main function...that is a no no, I'm afraid...you should declare like so:
#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.
Was This Post Helpful? 0
  • +
  • -

#5 bullet proof penguin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-November 05

Re: Prime number

Posted 15 November 2005 - 01:43 PM

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.
Was This Post Helpful? 0
  • +
  • -

#6 bullet proof penguin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-November 05

Re: Prime number

Post icon  Posted 16 November 2005 - 01:19 PM

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);
}

Was This Post Helpful? 0
  • +
  • -

#7 mikepetro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-November 05

Re: Prime number

Post icon  Posted 19 November 2005 - 12:28 AM

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:

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.

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 November 2005 - 12:32 AM

Was This Post Helpful? 0
  • +
  • -

#8 sonal.12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-September 08

Re: Prime number

Posted 14 September 2008 - 09:40 PM

View PostKcbroncofan, on 8 Nov, 2005 - 10:01 PM, said:

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
Was This Post Helpful? 0
  • +
  • -

#9 homemade-jam   User is offline

  • Gabe's Nemesis
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,300
  • Joined: 17-March 08

Re: Prime number

Posted 15 September 2008 - 03:37 AM

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
Was This Post Helpful? 0
  • +
  • -

#10 numerical_jerome   User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 167
  • Joined: 16-September 07

Re: Prime number

Posted 15 September 2008 - 08:20 PM

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:

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
Was This Post Helpful? 1

#11 whelpy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-June 09

Re: Prime number

Posted 05 June 2009 - 02: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:

#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:
Was This Post Helpful? 0
  • +
  • -

#12 Kanvus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: Prime number

Posted 05 June 2009 - 02:37 AM

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.
Was This Post Helpful? 0
  • +
  • -

#13 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Prime number

Posted 05 June 2009 - 03:03 AM

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.dreaminco...snippet2196.htm
Useful isn't it!
Was This Post Helpful? 0
  • +
  • -

#14 Kanvus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: Prime number

Posted 05 June 2009 - 03:12 AM

^u get a trophy for being most serious 2009
Was This Post Helpful? 0
  • +
  • -

#15 computerfox   User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Prime number

Posted 05 June 2009 - 02:12 PM

hope this helps
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2