Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads in an integer N and prints out all the prime numbers

ID: 3880140 • Letter: W

Question

Write a program that reads in an integer N and prints out all the prime numbers strictly less than N. These should be printed one per line.

sample input = 10

sample output= 2 3 5 7

my code :

#include<iostream>

using namespace std;

int main()

{
  
    int N, i;
  
  
  
    cin >> N;
  
    for (int i = 2; N >= i; ++i)
      
    {
      
        bool isPrime = true;
      
        for (int j = 2; j < ((i/2) + 1); ++j) // run the inner loop to i / 2 because
          
            // any factor should have to be atleast 2 times of main number
          
        {
          
            if (i % j == 0) //If the answer is 0 the statement is false and will be blank
              
            {
              
                isPrime = false;
              
                break;
              
            }
          
        }
      
        if (isPrime)
          
        {
          
            // why -+N ??
          
            cout << i << endl;
          
        }
      
    }
  
    return 0;
  
}

My code is correct but I get an error saying

when i submit it

Explanation / Answer

Hi

the program says all the prime numbers strictly less than N.

so when you gave 23 as N it is printing 23 also in the output. in order to correct

in line number 7 you have to remove (=)symbol in N>=i

for (int i = 2; N >= i; ++i) replace it to

for (int i = 2; N > i; ++i)

output before modification:

output after modification:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote