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.
1
Expert's answer
2016-01-19T08:28:40-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS