954,136 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Urgent: Plz Help me,Prime Number Program Problem

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

Attachments bonus.cpp (0.27KB)
Tiger909
Newbie Poster
2 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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

}
dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

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

Tiger909
Newbie Poster
2 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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.

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

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;
invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You