hi all,
I am new here,and I wish I could find a solution for this program
My program has to check if the number is prime or not?,
my program is working properly with numbers greater than 1 (n>1),
I want my program to display a friendly message when the user enter number (0) or (1) and then stop the program.
please Help,I tried many times to try to solve this problem but I couldn't !,,
mY hOmework is due on Saturday 29 Dec,
Please Help me

and this is my code :

#include <iostream>
using namespace std;
void main()
{
int x, y;
int z=0;
cout << "Please Enter a positive number\n";
cin >> x;
for (y = 2; y<x; y++)
{
if ((x % y) == 0)
{
z=1;
break;
}
}
if (z==1)
cout << "Not prime number\n";
else
cout << "prime number\n";
}

The problem with my program is when the user enter number 0 or 1 , it outputs is prime number:'( ,0 and 1 ARE NOT PRIME NUMBERS,,please help me and thanks for ur efforts

Try this

#include <iostream>
using namespace std;
void main()
{
	int x, y;
	int z=0;
	do{
		cout << "Please Enter a positive number\n";
		cin >> x;
		if(0 == x || 1 == x) {
			cout<<"Please try again with number other then 0 or 1" <<endl;
		}
	}
	while((0 == x) || (1 == x));

	for (y = 2; y<x; y++)
	{
		if ((x % y) == 0)
		{
			z=1;
			break;
		}
	}

	if (z==1)
		cout << "Not prime number\n";
	else
		cout << "prime number\n";

}

thanks a lot,I appreciate your efforts, it working now,
really,you are wonderful programmer,
Thankssssssssssssssssssss

It was easy, you will learn all these very soon.

You know for me it is very true. "There is always Room for Improvement :)"

Please mark the thread as solved.

Another solution:

#include <iostream>
using namespace std;
int main()
{
    int x, y;
    while (true) {
        cout << "Please Enter a positive number\n";
        cin >> x;
        if(0 != x || 1 != x) break;
        cout<<"Please try again with number other then 0 or 1\n";
    }

    for (y = 2; y<x; y++) {
        if ((x % y) == 0)
            break;
    }

    if (y < x)
        cout << "Not prime number\n";
    else
        cout << "prime number\n";
}

Since you want to check for only positive number and that number must not be 0 and 1, it is advisable to change this

if(0 != x || 1 != x) break;

to this:

if(x>1) break;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.