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>
using namespace 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

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.

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>
using namespace 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 ;)

Maybe this would interest you...

Maybe this would interest you...

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.