Urgent: Plz Help me,Prime Number Program Problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 2
Reputation: Tiger909 is an unknown quantity at this point 
Solved Threads: 0
Tiger909 Tiger909 is offline Offline
Newbie Poster

Urgent: Plz Help me,Prime Number Program Problem

 
0
  #1
Dec 24th, 2007
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
Last edited by Tiger909; Dec 24th, 2007 at 8:03 am.
Attached Files
File Type: cpp bonus.cpp (280 Bytes, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: Urgent: Plz Help me,Prime Number Program Problem

 
0
  #2
Dec 24th, 2007
Try this
  1. #include <iostream>
  2. using namespace std;
  3. void main()
  4. {
  5. int x, y;
  6. int z=0;
  7. do{
  8. cout << "Please Enter a positive number\n";
  9. cin >> x;
  10. if(0 == x || 1 == x) {
  11. cout<<"Please try again with number other then 0 or 1" <<endl;
  12. }
  13. }
  14. while((0 == x) || (1 == x));
  15.  
  16. for (y = 2; y<x; y++)
  17. {
  18. if ((x % y) == 0)
  19. {
  20. z=1;
  21. break;
  22. }
  23. }
  24.  
  25. if (z==1)
  26. cout << "Not prime number\n";
  27. else
  28. cout << "prime number\n";
  29.  
  30. }
Last edited by dubeyprateek; Dec 24th, 2007 at 9:13 am.
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 2
Reputation: Tiger909 is an unknown quantity at this point 
Solved Threads: 0
Tiger909 Tiger909 is offline Offline
Newbie Poster

Re: Urgent: Plz Help me,Prime Number Program Problem

 
0
  #3
Dec 24th, 2007
thanks a lot,I appreciate your efforts, it working now,
really,you are wonderful programmer,
Thankssssssssssssssssssss
Last edited by Tiger909; Dec 24th, 2007 at 9:45 am.
There is always Room for Improvement :),

Tiger 909
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: Urgent: Plz Help me,Prime Number Program Problem

 
0
  #4
Dec 24th, 2007
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.
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Urgent: Plz Help me,Prime Number Program Problem

 
0
  #5
Dec 24th, 2007
Another solution:

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int x, y;
  6. while (true) {
  7. cout << "Please Enter a positive number\n";
  8. cin >> x;
  9. if(0 != x || 1 != x) break;
  10. cout<<"Please try again with number other then 0 or 1\n";
  11. }
  12.  
  13. for (y = 2; y<x; y++) {
  14. if ((x % y) == 0)
  15. break;
  16. }
  17.  
  18. if (y < x)
  19. cout << "Not prime number\n";
  20. else
  21. cout << "prime number\n";
  22. }

Since you want to check for only positive number and that number must not be 0 and 1, it is advisable to change this
  1. if(0 != x || 1 != x) break;
to this:
  1. if(x>1) break;
Last edited by invisal; Dec 24th, 2007 at 10:41 am. Reason: Forget open brace
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC