Question #48572

write a program to display the prime numbers.

Expert's answer

#include <cstdlib>

#include <iostream>

using namespace std;

int main()

{

int i, j,S; //index

int N; //size of array

bool *mas; //bool array

cout << "Prime numbers till N \n";

cout << "Enter number N ";

cin >> S;

N=S+1;

mas= new bool [N]; //dynamic array N

for(i=1; i<=S; i++) mas[i]=true;

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

if(mas[i])

for(j=(i*i); j<=S; j+=i)

if(mas[j]) mas[j]=false;

//print array

for(i=1; i<=S; i++)

if(mas[i]) cout << i << " ";

system("pause");

return 0;

system("PAUSE");

return EXIT_SUCCESS;

}

#include <iostream>

using namespace std;

int main()

{

int i, j,S; //index

int N; //size of array

bool *mas; //bool array

cout << "Prime numbers till N \n";

cout << "Enter number N ";

cin >> S;

N=S+1;

mas= new bool [N]; //dynamic array N

for(i=1; i<=S; i++) mas[i]=true;

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

if(mas[i])

for(j=(i*i); j<=S; j+=i)

if(mas[j]) mas[j]=false;

//print array

for(i=1; i<=S; i++)

if(mas[i]) cout << i << " ";

system("pause");

return 0;

system("PAUSE");

return EXIT_SUCCESS;

}

## Comments

## Leave a comment