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
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
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
Your program produces unexpected outputs. The string: 23 should not be part of your output.
when i submit it
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
