I am writing a program that outputs whether or not a number is prime. Can anyone help me find where my syntax error is? Also, i get that i = 3; i <= num; i++ would be true for it being a prime number from the other forums I have read but I honestly don't get why, can someone explain? I get everything else I have done thus far.

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    int num;
    int i;
    
    cout << "Please enter a positive integer: " << endl;
    
    if (num <=1)
    return false;
else if (num == 2)
    return true;
else if (num % 2 == 0)
    return false;
else (i = 3; i <= num; i++);
    return true;
    
    system ("pause");

    return 0;
}

Also, i get that i = 3; i <= num; i++ would be true

I don't think it's true or false. It doesn't make sense. It's a loop, not a logical evaluation that evaluates to true or false. The evaluation part is the "i <= num" part. That's true or false, but the whole loop starter doesn't evaluate to true or false.

Presumably you want the word "for" in front of this and that's what the compiler is complaining about. But even if you stick "for" in front, it may or may not compile, but it still makes no sense. A loop means you want code to be executed repeatedly. WHAT code do you want to be executed repeatedly?

I guess I don't get exactly how the whole thing doesn't make sense, I wrote it up and then compared it to what others had and have something very similar.

I modified it but still feel I have a major error somewhere. Any ideas?

#include <iostream>
#include <iomanip>

using namespace std;
int main () 
{
int num;
int i;
cout << "Please enter a positive integer. Enter # to quit." << endl;
cin >> num;
if (num <=1)
cout << "Not a prime number" << endl;
else
{
for (i = 3; i <=num; i++)
cout << "Is a prime number." << endl;
{
 if (num % i == 0)
{
    cout << "Not a prime number." << endl;
    system ("pause");
return 0;
    }
}
}
system ("pause");
return 0;
}

I am writing a program that outputs whether or not a number is prime. Can anyone help me find where my syntax error is? Also, i get that i = 3; i <= num; i++ would be true for it being a prime number from the other forums I have read but I honestly don't get why, can someone explain? I get everything else I have done thus far.

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    int num;
    int i;
    
    cout << "Please enter a positive integer: " << endl;
    
    if (num <=1)
    return false;
else if (num == 2)
    return true;
else if (num % 2 == 0)
    return false;
else (i = 3; i <= num; i++);
    return true;
    
    system ("pause");

    return 0;
}

Vernon is saying that the code "else (i = 3; i <= num; i++);" is NOT a boolean expression. It can't be true or false.

What I think is required is a constant check on the number. Before referring to code outside, think about the problem itself.

Let's take a few minor examples for all numbers within the range [3-*)

Is 3 a prime number? Yes it is. Why? Because no other positive multiples exist to obtain 3 except 1*3 (only one set of multiples).
Is 4 a prime number? No it isn't. Why? Because there exist multiples 2*2 and 1*4.
Is 5 a prime number? Yes it is. Only set of multiples are 1*5.
6 isn't a prime number due to 1*6, 2*3 sets capable of generating the number 6.
....
15 isn't a prime number... 1*15, 3*5
17 is... 1*17...
18 isn't... 1*18, 2*9, 3*6

So basically you need to someone take a number and find out if it has either a multiple of 2 or some other multiple other than 1.

The loops statement improperly placed in the else condition check may actually be a hint as to how to check if a number is or isn't prime.

Taking a closer look, for 4, 6, 15 and 18, there exist multiples that are less than (or equal to) the square root of the actual number.

If your input is n (which can be any number) your loop only needs to loop sqrt(n) iterations. You can then use a parameter to check if the number is or isn't a prime number.

Example

//...

#include <math.h>

//...
// assuming all other tests passed
bool isPrime = true;

// n is the number being tested on
// i is the growing divisor
for(int i = 2; i <= static_cast<int>(sqrt(n)); i++){

  if( (n % i) == 0){
    isPrime = false;
    break;
  }
}

If at any time n / i has as 0 remainder, that means i was a multiple of n and since i is initially greater than 1 then there exists a another set of multiples that can generate n which would prove that n is not prime.

Source: http://en.wikipedia.org/wiki/Prime_number

The code may be error-prone, but hopefully you get the idea.

I modified it but still feel I have a major error somewhere. Any ideas?

#include <iostream>
#include <iomanip>

using namespace std;
int main () 
{
int num;
int i;
cout << "Please enter a positive integer. Enter # to quit." << endl;
cin >> num;
if (num <=1)
cout << "Not a prime number" << endl;
else
{
for (i = 3; i <=num; i++)
cout << "Is a prime number." << endl;
{
 if (num % i == 0)
{
    cout << "Not a prime number." << endl;
    system ("pause");
return 0;
    }
}
}
system ("pause");
return 0;
}

Between this code--

//...
if (num <=1)
cout << "Not a prime number" << endl;
// no check for num % 2
else
{
for (i = 3; i <=num; i++)
//...

-- you forgot to check for num % 2 before testing against i=3+

Other than that it's fine, but good luck testing on big numbers... squaring the number will save you significant runtime.

So I almost had it but was interrupted. This is now what I have

#include <iostream>
#include <iomanip>

using namespace std;
int main () 
{
int num;
int i;
cout << "Please enter a positive integer other than 1" << endl;
cin >> num;
if (i = 3; i <=num; i++)
cout << "Is a prime number" << endl;
else
{
 if (num % i == 0)
{
    cout << "Not a prime number." << endl;
    system ("pause");
return 0;
    }
}
system ("pause");
return 0;
}

I think you need to learn the proper syntax of coding first. There is nothing in C++ which is used like this..

if (i = 3; i <=num; i++)

It outputs both answers for all of my input, any help?

#include <iostream>
#include <iomanip>

using namespace std;
int main () 
{
int num;
int i;
int j;
cout << "Please enter a positive integer other than 1" << endl;
cin >> num;
for (i = 3; i <=num; i++);
    if (num % 2 != 0);
cout << "Is a prime number." << endl;

if(num%2 == 0);
    cout << "Not a prime number." << endl;
system ("pause");
return 0;
}

I have updated this a lot more and thought I would see if anyone had any advice.

#include <iostream>
#include <iomanip>

using namespace std;
int main () 
{
int num;
bool isPrime = true;
int i;
cout << "Please enter a positive integer other than 1" << endl;
cin >> num;
cout << "The number you entered is: " << num << endl;
cout << "  " << endl;
for (i = 2; i <=num; i++)
{
    if (num % i == 0);
    {
isPrime = false;
break;
}
}
if (isPrime == true)
{
cout << num << " is a prime number." << endl;
cout << "  " << endl;
}

else if (isPrime == false)
{
cout << num <<  " is not a prime number." << endl;
cout << "  " << endl;
}
system ("pause");
return 0;
}

I think have everything fixed except the fact it is outputting 1 as a prime number. if anyone can help with this that would be great.

#include <iostream>
#include <iomanip>

using namespace std;
int main () 
{
int num;
int i;
cout << "Please enter a positive integer other than 1" << endl;
cin >> num;
cout << "The number you entered is: " << num << endl;
cout << "  " << endl;
bool isPrime = true;
for (i = 2; i < num; i++)
{
    if (num % i == 0)
    {
isPrime = false;
break;
}
}
if (isPrime == true)
{
cout << num << " is a prime number." << endl;
cout << "  " << endl;
}

else if (isPrime == false)
{
cout << num <<  " is not a prime number." << endl;
cout << "  " << endl;
}
system ("pause");
return 0;
}

Between lines 10 and 11 you could add some error checking for the input.

cin >> num;
if(num <= 1){

   cout << "Invalid number entered. Exiting program.\n" << endl;
   return 0;
}else if(num == 2){
   cout << "2 is a prime number.\n" << endl;
   return 0;
}
cout << "The number you entered is: " << num << endl;

I have everything fixed but now can anyone give me guidance on how set it up to find whether it is prime or not by using the fact that the input number is not divisible by any odd integer less than or equal to the square root of the number if it is a prime number?

// This program tests to see whether an input number is a prime number or not
// it then outputs the number the user entered and states if it is or is not
// a prime number.

//Header Declaration
#include <iostream>
#include <iomanip>

using namespace std;
int main () 
{
    //Variable Declaration
int num;
int i;
//Request user input
cout << "Please enter a positive integer other than 1:" << endl;
cin >> num;
cout << "The number you entered is: " << num << endl;
cout << "  " << endl;
//Input the information needed to calculate whether the number is prime or not
if (num <=1)
//Set the output to state that 1 or a negative integer is a non-prime number
cout << num << " is not a prime number." << endl;

else
{ 
    for (i = 2; i < num; i++)
{
    if (num % i == 0)
{
    //Set the output to state that anything meeting the above conditions is a
    //non-prime number.
cout << num << " is not a prime number." << endl;
cout << "  " << endl;
system ("pause");
return 0;
}
}
//Set the output to state that anything not meeting the above conditions is a
//prime number.
cout << num <<  " is a prime number." << endl;
cout << "  " << endl;
}
system ("pause");
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.