# Thread: How do I do this?

1. ## 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.

2. 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

3. 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.

4. 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.

5. Originally Posted by twoners
EDIT - so much for going to sleep Mario F.
I am, dad!
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

6. 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

7. 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!!!

8. I'm used to C++, and it works in C and C++, so I didn't figure it would matter.

9. 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;
}```

Popular pages Recent additions