943,796 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2101
  • C++ RSS
Dec 24th, 2007
0

Urgent: Plz Help me,Prime Number Program Problem

Expand Post »
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
Attached Files
File Type: cpp bonus.cpp (280 Bytes, 29 views)
Last edited by Tiger909; Dec 24th, 2007 at 8:03 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tiger909 is offline Offline
2 posts
since Dec 2007
Dec 24th, 2007
0

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

Try this
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Dec 24th, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tiger909 is offline Offline
2 posts
since Dec 2007
Dec 24th, 2007
0

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

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.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Dec 24th, 2007
0

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

Another solution:

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. if(0 != x || 1 != x) break;
to this:
C++ Syntax (Toggle Plain Text)
  1. if(x>1) break;
Last edited by invisal; Dec 24th, 2007 at 10:41 am. Reason: Forget open brace
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Drawing Program
Next Thread in C++ Forum Timeline: Firewall blocking sockets





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC