Hello everybody,
I have just recently started learning C++ and I have come across my first hurdle. I have read, and re-read, researched online, but to no avail: I'm unable to comprehend how the code works and why it does. If someone would be kind enough to explain each step of the code, the reason it's there, the function of the statement, etc. it would be immensely appreciated!
Cheers,
Apprentice01 (Using Dev-C++ on WinXP)
#include <iostream>
#include <math.h>
using namespace std;
int main(){
int n, i, is_prime;
is_prime = true;
cout << "Enter a number to test primeness: ";
cin >> n;
i = 2;
while (i <= sqrt(static_cast<double>(n))){
if (n % i == 0)
is_prime = false;
i++;
}
if (is_prime)
cout << "Number is prime.";
else
cout << "Number is not prime.";
return 0;
}