# Answer to Question #56474 in C++ for Nithya

Question #56474

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

Solution

#include <iostream>

#include <math.h>

#define N 10

using namespace std;

bool isPrime(int k)

{

int i;

for(i=2;i*i<=k;i++)

if(k%i==0)

return false;

return true;

}

int main(int argc, char **argv)

{

int n;

cout<<"Input number: ";

cin>>n;

cout<<"Prime numbers: \n";

int k;

for(k=1;k<=n;k++)

if(isPrime(k))

cout<<k<<endl;

return 0;

}

#include <iostream>

#include <math.h>

#define N 10

using namespace std;

bool isPrime(int k)

{

int i;

for(i=2;i*i<=k;i++)

if(k%i==0)

return false;

return true;

}

int main(int argc, char **argv)

{

int n;

cout<<"Input number: ";

cin>>n;

cout<<"Prime numbers: \n";

int k;

for(k=1;k<=n;k++)

if(isPrime(k))

cout<<k<<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