Need a project done?

C++ Programming Developer

Search This Blog

C++: Whether a given Number is Prime or Not in C++




#include <iostream>
using namespace std;

int main()
{
int x; cin>>x;

for (int a = x-1; a>=2; a--)
{
if (x % a == 0)
{
cout << "Not Prime."<<endl;
exit(0);
}
}

cout<< "Prime"<<endl;

return 0;
}

//IF YOU WANT TO MAKE A FUNCTION, which checks for Prime Number, see the Program below.




#include <iostream>

using namespace std;
int f(int x)
{
int a = x;
while (--a>=2)
{
if ((x % a) == 0)
{
return 0;
}
}
return 1;
}

int main()
{
int x; cin>>x;

( f(x) )? cout <<"Prime": cout <<"Not Prime";

cout <<endl;
return 0;
}
"Don't let anyone ever make you feel like you don't deserve what you want."