Hey all,

I have been studying this exercise from "C++ Without Fear". I am new at C++ so it took me a while to follow through the whole program. I have a question though..

Why is var is_prime needed to be declared as int? The program used a static cast for double. Can't it be declared as double from the beginning?

It is just a little confusing to me.. Thanks guys and gals, here's the code:

Code:#include <stdafx.h> #include <iostream> #include <math.h> 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 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 by checking for divisibility by all whole numbers from 2 to sqrt (n).*/ i = 2; while (i <= sqrt(static_cast<double>(n))) //While i is <= sqrt(n), { if (n % i ==0) //If i divides evenly into n, is_prime = false; //n is not prime. i++; //Add 1 to i. } //Print results if (is_prime) cout << "Number is prime." << endl; else cout << "Number is not prime." << endl; return 0; }