please help Write a program PrimeNumbers in Java that receiv
please help
Write a program PrimeNumbers in Java that receives from the user a positive integer n and computes and prints all prime numbers up to n. Your program must use the following algorithm:
1. First insert all numbers from 2 to n into a set.
2. Erase all multiples of 2 (except 2); that is, 4, 6, 8, 10, 12, . . . .
3. Erase all multiples of 3 (except 3); that is, 6, 9, 12, 15, . . . .
4. Erase all multiples of 4 . . . . .
. Go up to n Then print the set.
Solution
#include<iostream>
#include<conio.h>
using namespace std;
// Class Declaration
class prime
{
//Member Varibale Declaration
int a,k,i;
public:
prime(int x)
{
a=x;
}
// Object Creation For Class
void calculate()
{
k=1;
{
for(i=2;i<=a/2;i++)
if(a%i==0)
{
k=0;
break;
}
else
{
k=1;
}
}
}
void show()
{
if(k==1)
cout<<\"\ \"<<a<<\" is Prime Number.\";
else
cout<<\"\ \"<<a<<\" is Not Prime Numbers.\";
}
};
//Main Function
int main()
{
int a;
cout<<\"Enter the Number:\";
cin>>a;
// Object Creation For Class
prime obj(a);
// Call Member Functions
obj.calculate();
obj.show();
getch();
return 0;
}

