0

I'M working my way through the book "C++ without fear by Brian Overland" and there are two small programs with to totally different types of type casting and the book fails to explain the difference, please help me understand. Thanks. The first one is on a while loop and the second one is on a for loop.

prime1.cpp

```
using namespace std;
int main()
{
int n; // number to test for prime-ness
int i; // loop counter
int is_prime; // boolean flag
// assume that a number is prime untio proven otherwise
is_prime = true;
// get a number from the keyboard
cout<< "Enter a number and press ENTER: ";
cin>> n;
// test for primeness by checking for divisiblity by all whole numbers from 2 to sqrt(n)
i = 2;
while(i <= sqrt(static_cast<double>(n)))
{
if(n % i == 0)
is_prime = false;
i++;
}
// print results
if(is_prime)
cout<< "Number is prime.";
else
cout<< "Number is not prime.";
system("pause");
return 0;
}
```

prime2.cpp

```
#include <Stdafx.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n;
int i;
int is_prime;
// assume that a number is prime until proven otherwise
is_prime = true;
// get a number from the keyboard
cout<< "Enter a number and press ENTER: ";
cin>> n;
// test for prime-ness
for(i = 2; i <= sqrt((double) n); i++)
{
if(n % i == 0)
is_prime = false;
}
// print results
if(is_prime)
cout<< "Number is prime.";
else
cout<< "Number is not prime.";
system("pause");
return 0;
}
```