PDA

View Full Version : C++ prime numbers query


lonewolf_860
03-29-2005, 08:06 AM
i am trying to make a C++ prog. When i enter a number it will test if the number is a prime number

if it is it will display Prime... if not it will display not prime...pretty simple

so far:

#include <iostream>
#include <cstdlib>
using namespace std;

int main ()
{
int number; //user inputed number

cout << "enter the number";
cin >> number;

if(number % 2 == 0 || % 3 == 0 || % 5 == 0 )
cout << "not prime" << endl;
else
cout << "Prime";


system ("PAUSE");
return 0;
}

okay everytime i try to run it it says

"parse error before %" can anyone help me with this.......as you can prob tell with my coding i am a beginer

EDIT* it also says "confused by earlier errors, bailing out" underneath the parse error

lonewolf_860
03-29-2005, 08:15 AM
ok i have solved my problem using some simple else if functions

#include <iostream>
#include <cstdlib>
using namespace std;

int main ()
{
int number; //user inputed number

cout << "enter the number";
cin >> number;

if(number % 2 == 0)
cout << "not prime" << endl;
else if(number % 3 == 0)
cout << "not prime";
else if(number % 5 == 0)
cout << "not prime";

else
cout << "Prime";

system ("PAUSE");
return 0;
}

it looks kinda messy is there another way of doing this??

thanks

lonewolf_860
03-29-2005, 09:29 AM
okay now i want to veiw the number of prime numbers between 2 numbers
can anyone help me do this by expanding on the program i have written??


eg the user inputs the numbers 10 and 0

so the program should display 5 ask that is the number of prime numbers between 10 and 0 (i think)

thanks any help would be useful

Dr. Evil
03-29-2005, 01:28 PM
If you're getting into calculating prime numbers I'd recomment this (http://www.troubleshooters.com/codecorn/primenumbers/primenumbers.htm) site as somewhat of a starter. I do not wish to look as if I am promoting myself, but I've created a few examples of a prime number algorithm myself which you can find here (www.cjb.cc/members/nick_e/index.html#prime), if those help at all.

By the way, use getchar() instead of system("pause"), uses less resources.

Unit
03-29-2005, 08:12 PM
your original problem with % can be solved by using number each time you want to take a modulus

if((number % 2) == 0 || (number % 3) == 0) || (number % 5) == 0 )

renmorrow
07-17-2009, 06:57 PM
I saw your thread, here is a code i wrote.. the problem with the one your working on, is that when you type 2,3,5 it reads not prime,,,... but they are prime.



#include <iostream>

using namespace std;

int main()
{
int prime;

cout<<"Input a number, to find out \n\
if it is a prime number."<<endl;


cin>>prime;

if(prime==1)cout<<"You would think 1 would be a prime number, but it's not"<<endl;
else if(prime==2)cout<<"2 is the only even prime number"<<endl;
else if(prime==3)cout<<"Is a prime number"<<endl;
else if(prime==5)cout<<"Is a prime number"<<endl;
else if(prime%2==0||prime%3==0||prime%5==0)cout<<"Is not a prime number"<<endl;
else cout<<"is a prime number"<<endl;


return 0;

}

adarshakb
07-17-2009, 08:42 PM
use this logic to check if a number is prime....

int isPrime=1;

for(int i=2;i<(num/2);i++)
{
if(num%i==0){
//not prime number
isprime=0;break;}
}


the code should work for all numbers >0

holyfire001202
01-21-2011, 06:43 AM
i am trying to make a C++ prog. When i enter a number it will test if the number is a prime number

if it is it will display Prime... if not it will display not prime...pretty simple

so far:

#include <iostream>
#include <cstdlib>
using namespace std;

int main ()
{
int number; //user inputed number

cout << "enter the number";
cin >> number;

if(number % 2 == 0 || % 3 == 0 || % 5 == 0 )
cout << "not prime" << endl;
else
cout << "Prime";


system ("PAUSE");
return 0;
}

okay everytime i try to run it it says

"parse error before %" can anyone help me with this.......as you can prob tell with my coding i am a beginer

EDIT* it also says "confused by earlier errors, bailing out" underneath the parse error



Here's the one I saved from earlier today, may help, may not, but it works perfect.


#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;

int main()
{
int number;
int count = 0;
cout<< "This program finds out if the number you enter is a prime"<<endl;
cout<<"Enter a number to test: ";
cin>>number;
int i;
cout<<"the factors of "<<number<<" are: "<<endl;
for(i = 2; i <=number; i++){
if((number%i) ==0){
count =count+1;
cout<<i<<" ";

}
}
cout<<endl;
if (count==1){
cout<<number<<" is a prime."<<endl;
}
else
cout<<number<<" is not a prime. It has "<<count<<" factors.\n";

system("pause");
return(0);
}

jonrhaider
07-18-2011, 12:28 AM
So I just started learning c++ online last week, and I've been able to write a couple simple "prime number" programs.
One is a test of whether a particular inputted number is prime, and the other is a program that outputs all prime number within a range...

Here's the first one:

#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
do
{
unsigned long int x;
cout << "Please enter a positive integer: ";
cin >> x ; // user inputted number
if( x == 2 ) cout << " \n \n Prime number \n \n \n" ; //2 is somewhat of an exception
for ( unsigned long int y = 2 ; y < x ; y++ ) //start by trying to divide by 2 and work your way up until you reach x
{
if( ( x % y == 0 ) ) // if it divides with no remainder, then y is a factor
{ cout << " \n Non-prime \n \n Because " << x << " / " << y << " = " << x/y << endl << endl << endl ;
break; // display Non-Prime, the reason why and exit the loop
}
if( y == x - 1 ) // if y has gone through all numbers until x-1 and no factors, x is prime
{ cout << " \n \n Prime number \n \n \n";
break; //display Prime and exit the loop
}
}
} while (true);
return 0;
}



Here's the second one:



#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
do
{
unsigned long a; // "a" is the lower limit
unsigned long b; // "b" is the upper limit
unsigned long x; // "x" is a test variable
cout << "\n \n Please enter the lower limit: ";
cin >> a;
cout << "\n \n Please enter the upper limit: ";
cin >> b;
x = a;
if (x <= 2) // since the number "2" is somewhat of an exception to the rule of prime numbers
cout << "2" << endl;
for( ; x <= b ; x++ ) // "x" starts at value "a" and should only continue (looping) up to value "b"
{
for ( unsigned long y = 2 ; y < x ; y++ )
// "y" is introduced to test whether x has any divisors
// "y" starts at "2" and goes all the way up to "x-1"
{

if( x % y == 0) // if there is no remainder, then they're divisible and "x" is NOT prime
// in which case, we should exit this loop and check the next number "x+1"
break;
else
{
if(( y == x - 1)) // otherwise, check if "y" has gone through all numbers up to "x-1"
cout << x << endl; // if "y" has reached "x-1" and still no dividents, then "x" is prime
}
}
}
} while (true);
return 0;
}



I'm sure there are easier ways to write these, but they don't seem too bad... I hope someone finds this useful (they took me a few hours)!

Jon