#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout<<"please enter a value to be evaluated as prime or not prime";
int x=0;
cin>>x;
int loop=2;
int prime=true;
while(loop == sqrt(x));<----this is the error.
{
if (x % loop==0)
prime=false;
loop++;
}
if(prime)
cout<<"number is prime";
else
cout<<"number is not prime";
system("PAUSE");
return 0;
}
thank you for you're time.
error is in sqrt()
Last edited by vegiestirfrypizza on Sun Mar 25, 2012 1:21 am, edited 1 time in total.
the morning is good for algorithms and tough thinking, the night is for things that don't require such forethought.
First off the while([condition]) line should not end with a semi-colon. Second, sqrt() returns a double or a float depending on what type you pass as a parameter.
To get your code to compile you could simply cast the return value to an int and remove the semi-colon:
thanks, i actually got it from a c++ book, maybe its not the best resource...not the assignment every line of code you see is in fact, not actually mine although it would be just as bad. but yeah, thank you for a better prime number code, and thanks again.
the morning is good for algorithms and tough thinking, the night is for things that don't require such forethought.