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.
1
Expert's answer
2015-11-23T04:14:34-0500
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;
}

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
APPROVED BY CLIENTS