How do I do this?

• 07-24-2006
ldb88
How do I do this?
Is it possible (or practical) to do this without using exit() or goto? And do I need the default case, since there is only one case I am worried about? I have a version using if(), would it be more efficient to use that instead? Do I need the break after exit() since the program will have ended anyway?
Code:

```#include<iostream> using namespace std; int main() {         int n;         int i;         printf("Please enter an integer\n");         scanf("%d",&n);         for(i=2;i<n;i++)         {                 switch(n%i)                 {                 case 0:                         printf("%d is not a prime\n",n);                         exit(0);                         break;                 default:                         break;                 }         }         printf("%d is a prime\n",n);         return 0; }```
Thanks.
• 07-24-2006
Mario F.
Code:

```int main() {         int n;         int i;         printf("Please enter an integer\n");         scanf("%d",&n);         for( i=2; i < n; ++i ) {             if( n % i == 0 ) {                 printf("%d is not a prime\n",n);                 break;             }         }         if( i == n )             printf("%d is a prime\n",n);         return 0; }```
Note: no error checking is done
• 07-24-2006
twomers
Something like this ?

Code:

```#include<iostream> using namespace std; int main() {         int n;         int i;         bool isprime = true;         cout<< "Please enter an integer\n";         cin >> d;         for( i=2; i<n; i++ )        // I think it only needs be n/2 really.         {        // If a number is being divided by something that's over half its value, it can't be whole!                 if ( ( n%i ) == 0 )                 {                         isprime = false;                 }         }         switch ( isprime )         {                 case true;                         cout<< d << " is a prime!";                         break;                 case false;                         cout<< d << "is not a prime number :(";                         break;         }         return 0; }```

I haven't compiled it, so I don't know if it works. Why did you use printf and scanf?

EDIT - so much for going to sleep Mario F.
• 07-24-2006
ldb88
Thanks to both of you. Especially Mario F. - my if() version was exactly like that, except I didn't think to put an if() for the bottom printf. And I used printf and scanf because I thought of this while doing Thinking in C by Bruce Eckel and he uses a lot of printf and scanf in the lecture.
• 07-24-2006
Mario F.
Quote:

Originally Posted by twoners
EDIT - so much for going to sleep Mario F.

Just finishing some stuff now that I finally had the whole thing working. It may not be much, but it really felt good to finally understand about memory management and have put it to work :)
• 07-24-2006
twomers
You could always put in something to check if it's an even number so that you wouldn't have to go through the whole checking all the variable malarky
• 07-24-2006
Richie T
There's a pretty big mix up in variables in twomers' code, change all occurances
of d to n, also in the switch cases, it should be

case true:

not

case true;

Other than those it works fine (assuming a well behaved user ;))

EDIT: If you are reading "Thinking In C", why did you post in the C++ forum?
Make sure you know what language you are trying to learn!!!
• 07-24-2006
ldb88
I'm used to C++, and it works in C and C++, so I didn't figure it would matter.
• 07-24-2006
Dave_Sinkula
Putting your code into a function would be something else to consider.
Code:

```#include <cstdio> using namespace std; bool foo(int n) {   for ( int i = 2; i < n; ++i )   {       if ( n % i == 0 )       {         return 0;       }   }   return 1; } int main() {   for ( int n = 1; n < 20; ++n )   {       printf("%d is %sa prime\n", n, foo(n) ? "" : "not ");   }   return 0; }```