68 399
Assignments Done
98,5%
Successfully Done
In December 2018

Answer to Question #48572 in C++ for umerinayat

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;
}

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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions