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

Dani AI

Generated

Quick summary and what went wrong (brief): , your program prints numbers past the user’s input because the loop and condition are set up incorrectly. was right that testing only divisibility by 2 and 3 is insufficient; there’s also a subtle C/C++ bug in your code: if (n % 2 != 0, n % 3 != 0) uses the comma operator and only evaluates the second test. Also you start from n (the limit) and then increment it inside the loop, which turns the loop into the opposite of what you want. tried a nested loop idea, but it doesn’t implement a correct primality check either.

Two simple, reliable options to fix this

  • Trial division (good for a single number or modest limits): for each candidate from 2 to limit, test divisibility by integers up to sqrt(candidate). Stop at the first divisor.
  • Sieve of Eratosthenes (best when you want all primes up to N): mark multiples and print the remaining primes. It’s fast and simple to implement.

Example: a compact Sieve implementation you can drop into a C++ program:

#include <iostream>
#include <vector>

void print_primes_up_to(int limit) {
    if (limit < 2) { std::cout << "No primes.\n"; return; }
    std::vector<char> is_prime(limit+1, true);
    is_prime[0] = is_prime[1] = false;
    for (int p = 2; p*p <= limit; ++p)
        if (is_prime[p])
            for (int m = p*p; m <= limit; m += p) is_prime[m] = false;

    for (int i = 2; i <= limit; ++i)
        if (is_prime[i]) std::cout << i << ' ';
    std::cout << '\n';
}

Quick debugging checklist

  • Don’t modify the user’s limit variable while iterating candidates. Use a candidate loop: for (int cand = 2; cand <= limit; ++cand).
  • Don’t print the header inside the loop.
  • Validate input (reject <= 1).
  • For very large limits use the sieve; for single-number primality checks use trial division with sqrt.
  • Test with small cases: 1, 2, 3, 22, 100 to confirm correctness.

Note on originality: avoid copying full solutions from others (see ); write and understand the code you submit.

Recommended Answers

All 4 Replies

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.