# Answer to Question #56560 in C++ for F

Question #56560

Write a program that asks the user to enter a positive number then displays all numbers less or equal to it that are prime each one a separate line.

Expert's answer

#include <iostream>

using namespace std;

int main()

{

int n = 0;

cout<<"Please enter n: ";

cin>>n;

for(int number_test = 2; number_test <= n; number_test++){

int isPrime = 1;

for(int divisor = 2; divisor < number_test; divisor++){

if(number_test % divisor == 0){

isPrime = 0;

break;

}

}

if(isPrime)

{

cout << number_test << endl;

}

}

return 0;

}

using namespace std;

int main()

{

int n = 0;

cout<<"Please enter n: ";

cin>>n;

for(int number_test = 2; number_test <= n; number_test++){

int isPrime = 1;

for(int divisor = 2; divisor < number_test; divisor++){

if(number_test % divisor == 0){

isPrime = 0;

break;

}

}

if(isPrime)

{

cout << number_test << endl;

}

}

return 0;

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment