954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Finding Prime Numbers

Hey again I need a little help with this assignment. What I'm supposed to do is have a user enter a positive integer and then the program should tell the user all the prime numbers before the number the user entered. However, with my program when I enter 22 for example it will give me numbers like 25, 26, and 28, or something along those lines.

Here's my code:

#include<iostream>
#include<string>
usingnamespace std; 
int main ()
{
int choice;
 

cout<<"What would you like to do: "<<endl;
cout<<"Make your choice by entering 1, 2, 3, or 4  "<<endl;
cout<<"+-------------------------------+"<<endl;
cout<<"1. Enter a positive integer, then print out all the prime numbers that are less than or equal to the number you entered"<<endl;
cout<<"2. Enter a positive integer and then print out the number with its digits reversed"<<endl; 
cout<<"3. Enter two positive integers and then print out the greatest common factor"<<endl;
cout<<"4. Nothing"<<endl;
cout<<"+-------------------------------+"<<endl;
cin>>choice;
 

if(choice==1)
{
int n=0;
 
cout<<"Please enter a positive integer "<<endl;
cin>>n;
 

while( n > 1)
{
if( n % 2 != 0, n % 3 != 0)
{

cout<<"The prime numbers are: "<<n<<endl;

n++;
 
if(n % 6 == 0)
{
n++;
}
}
}
}


Thanks again for the help

Rickenbacker360
Newbie Poster
21 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

First of all, what did you do to get the code to print with colors like you did?

And has anyone explained code formatting? Indentation is necessary to understand code.

The problem is you don't seem to know how to test for prime numbers.
You actually have to test the number to see if it's prime from 2 up to the number (shorter is possible, but not yet). You can't just test if it's divisible by 2 and 3. There's also 5, 7, 11, etc.

And after you test the value you increment the number you input, so if you entered 22 you end up testing 22, 23, 24, 25, ..., infinity.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Hey again I need a little help with this assignment. What I'm supposed to do is have a user enter a positive integer and then the program should tell the user all the prime numbers before the number the user entered. However, with my program when I enter 22 for example it will give me numbers like 25, 26, and 28, or something along those lines. Here's my code:

#include<iostream>
#include<string>
usingnamespace std; 
int main ()
{
int choice;
 

cout<<"What would you like to do: "<<endl;
cout<<"Make your choice by entering 1, 2, 3, or 4  "<<endl;
cout<<"+-------------------------------+"<<endl;
cout<<"1. Enter a positive integer, then print out all the prime numbers that are less than or equal to the number you entered"<<endl;
cout<<"2. Enter a positive integer and then print out the number with its digits reversed"<<endl; 
cout<<"3. Enter two positive integers and then print out the greatest common factor"<<endl;
cout<<"4. Nothing"<<endl;
cout<<"+-------------------------------+"<<endl;
cin>>choice;
 

if(choice==1)
{
int n=0;
 
cout<<"Please enter a positive integer "<<endl;
cin>>n;
 

while( n > 1)
{
if( n % 2 != 0, n % 3 != 0)
{

cout<<"The prime numbers are: "<<n<<endl;

n++;
 
if(n % 6 == 0)
{
n++;
}
}
}
}

Thanks again for the help


=======================================
Hi dear,

U can try using this simple logic to display the Prime number till user enter the number.

int userNum;
int div;

for(i=2;i<userNum;i++)
{
for(div=userNum/2; div>1;div--)
{
res=userNum%div;

if(res!=0)
printf("%d", res);

}
}


regards,
Manoj Sah ;)

Manojsah
Newbie Poster
13 posts since Aug 2005
Reputation Points: 10
Solved Threads: 2
 

Maybe this would interest you...

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
Maybe this would interest you...

I think his TA would look right through that as somebody else's fairly sophisticated code!

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You