Hi ehsan687. There are some notes that I hope to be useful.
1) Never call
main() function. Choose another way to do what you want.
2) It is not required to use ; after {} of loops or function implementations.
3) Be aware of using = and ==. The first is assignment operator and the second is for checking equally.
4) Always think before you begin to write codes. This will reduce your mistakes and will improve your code.
5) In programming world, when you have to do a most repetitive job, often there is an easier way. For example look at your code(lines 25-42). The three case statements are doing same work and just the conditions are different. Think about it 20 seconds...
We can replace those codes with just one of them and use OR operator in the condition.
for this purpose, we should write case statements without
break;
and
{ }
I mean:
1 2 3 4 5 6
|
case 1:
case 2:
case 3:
cout << "The number u entered is a prime number." ;
cout <<endl<<endl;
return;
|
for information about Switch-case go to this link:
http://www.cplusplus.com/doc/tutorial/control/
6) Always before start coding, try to get more and more information about your problem. A programmer should study/get information/ask somebody/etc before he/she begins to coding.(See comments in codes below. Those are information I have get from math)
For example in your problem(specifying that input number is prime or not) there are some algorithms that make coding easier. (look at this code for example)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <iostream>
using namespace std;
bool basic_check (int); /** This function takes a number and checks whether it's prime or not.
if the number is prime, function will return true.
if the number is not prime, function will return false.
**/
int main()
{
int input;
bool result;
while(1){ /// An infinite loop for repeat program. It will stop when user enters 0
cout<<"Enter a number: ";
cin>>input;
if(input==0)
break;
else if(input<0)
cout<<"Invalid input."<<endl<<endl;
else{
result=basic_check(input);
if(result)
cout<<"The number is prime."<<endl<<endl;
else
cout<<"The number is not prime."<<endl<<endl;
}
}
return 0;
}
bool basic_check(int input){
/** Mathematical note:
1) A number is prime if divided only by 2 numbers.
2) The dividers of a number are not greater than half of it.
e.g. the dividers of 200 are not greater than 100.
**/
int i,dividers=0; ///dividers is a variable that we store sum of dividers in it.
for(i=1;i<=input/2;i++)
if(input%i == 0)
dividers++;
if(dividers==2)
return true;
else ///we can erase this line. It is just for more readability
return false;
}
|
Or use the code below for checking primness:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
bool basic_check(int input){
/** Mathematical note:
1) A number is not prime if there is at least one divider except 1 and itself.
2) The dividers of a number are not greater than half of it.
e.g. the dividers of 200 are not greater than 100.
**/
int i;
for(i=2; i<=input/2; i++)
if(input%i==0)
return false;
return true;
}
|
After all these advises(or maybe boring advises!) look at this short code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
using namespace std;
bool isprime(int);
int main(){
int input;
while(1){
cout<<"Enter a number: ";
cin>>input;
if(input==0)
break;
else if(input<0)
cout<<"Invalid input."<<endl<<endl;
else if(isprime(input))
cout<<"The number is prime."<<endl<<endl;
else
cout<<"The number is not prime."<<endl<<endl;
}
return 0;
}
bool isprime(int input){
for(int i=2; i<=input/2; i++)
if(input%i==0)
return false;
return true;
}
|
Note: the manner you choose for your program depends on the problem you have faced with.