| | |
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:
Solved Threads: 0
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 :
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
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.
Try this
C++ Syntax (Toggle Plain Text)
#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"; }
Last edited by dubeyprateek; Dec 24th, 2007 at 9:13 am.
I know I am. Therefore I am.
Another solution:
Since you want to check for only positive number and that number must not be 0 and 1, it is advisable to change this
to this:
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
if(0 != x || 1 != x) break;
C++ Syntax (Toggle Plain Text)
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
Behind every smile is a tear.
Visal .In
![]() |
Other Threads in the C++ Forum
- Previous Thread: Drawing Program
- Next Thread: How do I find out how many digits in an integer?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





"