Answer to Question #6548 in C++ for karan shah

Question #6548
write a program that does prime numbers test ... for result if number is prime it shows 1 and if number is not prime then result is 0.
1
Expert's answer
2012-02-14T10:43:40-0500
#include <iostream>

using namespace std;

void main()
{
int A[99999];
int n;
cout << "Enter the number: ";
cin >> n;

for(int i = 2; i <= n; i++)
& A[i] = 1;

for(int i = 2; i*i <= n; i++)
{
& if(A[i] == 1)
& {
& int j = i*i, k = 1;
& while(j <= n)
& {
& if(A[j] == 1)
& A[j] = 0;

& j = i*i + k*i;
& k++;
& }
& }
}

if(A[n] == 1)
& cout << n << " - Is prime!";
else
& cout << n << " - Is not prime!";

}

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