Hi, I need help with some C++, I bought a book and there's an example code which says if the number is prime or not, but it can only calculate the numbers 1, 2 and 3.Whenever I type in some other number it just freezes and doesn't do anything.
Here's the code, I want to know what I'm doing wrong:
[CODE]
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
int i;
int is_prime = true;
cout << "Type in a number and press enter" << endl;
cin >> n;
i = 2;
while( i <= sqrt(double (n)) );
{
if( n % i == 0 )
{
is_prime = false;
i++;
}
}
if(is_prime)
cout << "Number is prime" << endl;
else
cout << "Number isn't prime" << endl;
system("pause");
return main();
}
[/CODE]
I would really appreciate if someone could help me with this.Thanks in advance.
[highlight](User was banned for this post ("undescirptive thread title" - postal))[/highlight]
The semicolon at line 15 (after the while) is incorrect. Remove it.
All these easy beginner question threads recently makes me want to make a megathread so I can help people in a central place, rather than having to seek them out, or hope they find me xD
[QUOTE=Corewarp3;40360593]All these easy beginner question threads recently makes me want to make a megathread so I can help people in a central place, rather than having to seek them out, or hope they find me xD[/QUOTE]
[url]http://facepunch.com/showthread.php?t=1250528[/url]
Sorry, you need to Log In to post a reply to this thread.