#include<iostream>#include<math.h>usingnamespacestd;intmain(){doublen,i;// Declaring variable i and n boolis_prime=true;// Boolean flag; assume true until proven otherwisecout<<"Enter a number and press ENTER: ";cin>>n;for(i=2.0;i<=sqrt(n);i++)// Test for prime by checking for divisibility by all whole numbers from 2 to sqrt(n).{if(n%i==0.0)boolis_prime=false;}if(is_prime=true){cout<<"Number is prime."<<endl;}else{cout<<"Number is not prime."<<endl;}system("PAUSE");return0;}
It is to show weather a number is prime or not.
On line 14:
{if (n % i == 0.0)
it says it is an invalid operant to types 'double and 'double'.
Please help, do I have to declare i or n as a float, int, long float, long
int, or long double? DAMNIT!!!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
#include<iostream>#include<math.h>#include<cmath>usingnamespacestd;intmain(){doublen,i;// Declaring variable i and n boolis_prime=true;// Boolean flag; assume true until proven otherwisecout<<"Enter a number and press ENTER: ";cin>>n;for(i=2.0;i<=sqrt(n);i++);// Test for prime by checking for divisibility by all whole numbers from 2 to sqrt(n).iffmod(n/i)==0;{is_prime=false;}if(is_prime=true){cout<<"Number is prime."<<endl;}else{cout<<"Number is not prime."<<endl;}system("PAUSE");return0;}
to have line 15
if fmod(n/i) == 0; {
[code]
have a parse error before '(' token, so now its easier to fix, though I still don't know how. Please help!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This is my program:
It is to show weather a number is prime or not.
On line 14:
it says it is an invalid operant to types 'double and 'double'.
Please help, do I have to declare i or n as a float, int, long float, long
int, or long double? DAMNIT!!!
You can't use the modulo operator '%' on doubles, it can only be used on
integers.
Take a look at fmod from the math library.
So do I have to include fmod?
Dev-C++ won't let me declared n as an int though, so what do I do?
Thanks jimp03, I changed the code tho this:
to have line 15