# Nested loop help

Page 1 of 1

## 3 Replies - 2154 Views - Last Post: 10 February 2009 - 06:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=85982&amp;s=c4e26df4c52e9aa23945361b7b1df1be&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 8100 Power

• New D.I.C Head

Reputation: 0
• Posts: 21
• Joined: 10-February 09

# Nested loop help

Posted 10 February 2009 - 05:58 PM

This is a really simple program, but I'm struggling.

I'm writing a nested loop program that determines if the inputted number is prime, I need to perform this over and over until the user inputs 0 or a negative number.

Here is what I have so far (int main code):

```cout << "Please enter digits to determine if prime or not, use 0 to quit"<< endl;
cin >> input; // I know not to have the user input twice, but, how can I preform a check first if the number isn't a negative or a zero?

while( input != 0 && input > 0){

cin >> input;

while(what expression){   // <---This is my problem?? reference below!

input = input % 2;  // This process and gives a 1 if the number is prime

if ( input == 1)
cout << "The Number is Prime" << endl;
else
cout << "The Number is not prime" << endl;
}

}
```

You can see where I referenced my problem(s). I'm new, just looking for help and suggestions! Thanks

Is This A Good Question/Topic? 0

## Replies To: Nested loop help

### #2 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: Nested loop help

Posted 10 February 2009 - 06:14 PM

This was done awhile a go for a student ...

It may demo several new things?

Shalom,
David

```#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

// Global constants
const int MAX = 1000;
const int MIN = 1;

// function prototypes
bool again();

int main()
{
do
{
cout << "Enter a # in range " << MIN << ".." << MAX
<< " to see all prime #'s up to that #: ";
int num = 0;
cin >> num;
cin.clear(); // in case error flag set if non integer entered
cin.sync(); // to eat up '\n' chars, etc., in cin buffer
if (num < MIN || num > MAX)
{
cout << "You did not follow the input rules." << endl;
continue; // added here to jump to end of do ... while loop test
}
else cout << "Input accepted." << endl;

}while( again() );
}

// *** cam start with 3 and increment by 2 ... since even numbers are NOT prime  ***
{
cout << "\nPrimes:\n\n" << setw(4) << 1 << " ";  // print out first manually
if( num > 1 )  cout << setw(4) << 2 << " ";
for( int i = 3; i <= num; i += 2 ) // can test only odd numbers ...
{
bool isprime = true;

// test if this 'i' is prime ... by dividing by 3,5,7 ...
for( int j = 3; j <= sqrt(i); j += 2 ) // can use only odd divisors up to sqrt(i)
{
if( i % j == 0 ) // i MOD j equal to zero
{   isprime = false;
break;// break OUT NOW from INNER for loop, since NOT prime
}
}
if( isprime ) cout << setw(4) << i << " "; // outputs prime number to screen
}
cout << endl << endl;
}

bool again()
{
cout << "Do you want to run the program again (y/n) ? ";
int answer = cin.get();
cin.sync(); // 'flush' cin
if( answer == 'n' || answer == 'N' )
{
cout << "Press 'Enter' to exit ... " << flush;
cin.get();
return false;
}
// if reach here ...
return true;
}
```

### #3 crazyjugglerdrummer

• GAME OVER. NERD WINS.

Reputation: 124
• Posts: 690
• Joined: 07-January 09

## Re: Nested loop help

Posted 10 February 2009 - 06:18 PM

What you really want to know is when to stop taking the remainder of input divided by two. You would want to continue until input==0 || input==1. But the procedure would only need to run once, because after %2, input is either one or zero no matter what.

However a loop structure is the right way to go.

(If I may contribute some additional advice):

Finding primes is (sadly) slightly more complicated than checking if the number is divisible by two. This will find if the number is odd, which is the right track to be on because all primes (except for 2) are odd. You would then have to check if the number was divisible by 3,5,7, etc. Take the number 9. 9%2 equals 1, but nine isn't prime.

I would also recommend using the Sieve of Eratothenes as another solution.

And welcome to dream in code!!!!

This post has been edited by crazyjugglerdrummer: 10 February 2009 - 06:19 PM

### #4 8100 Power

• New D.I.C Head

Reputation: 0
• Posts: 21
• Joined: 10-February 09

## Re: Nested loop help

Posted 10 February 2009 - 06:38 PM

crazyjugglerdrummer, on 10 Feb, 2009 - 05:18 PM, said:

What you really want to know is when to stop taking the remainder of input divided by two. You would want to continue until input==0 || input==1. But the procedure would only need to run once, because after %2, input is either one or zero no matter what.

However a loop structure is the right way to go.

(If I may contribute some additional advice):

Finding primes is (sadly) slightly more complicated than checking if the number is divisible by two. This will find if the number is odd, which is the right track to be on because all primes (except for 2) are odd. You would then have to check if the number was divisible by 3,5,7, etc. Take the number 9. 9%2 equals 1, but nine isn't prime.

I would also recommend using the Sieve of Eratothenes as another solution.

And welcome to dream in code!!!!

Thanks.. The code above is way out of my league right now.

We're asked to use just two loops and perform a check if it falls into the correct format ( input != 0 && input > 0).

Any suggestions on how to implement what I have into my two loops?

Would I cin >> input in the outerloop? , also when I do that, it acts like it skips as if the input wasn't even there.