Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed Why is my program not working? Why is the error telling me to put ';' before ')'?

+3 votes
asked Feb 6, 2022 by 7B 12 ANTARIKSH MONDAL (150 points)
closed Apr 8, 2022 by Admin
#include <iostream>
using namespace std;

int main()
{
    int n=0, i=2
    ;bool isPrime=true
    
    ;cout<<"Enter the integer:";
    cin>>n;
    
    if (n==0 || n==1) {
        isPrime=false;
    }
    else {
        for (i=2, i<=n/2, ++i) {
            if (n%i==0) {
                isPrime=false;
                break;
            }
        }
    }
    if (isPrime) {
        ;cout<<"Your integer is a prime number."<<n<<endl;
    }
    else {
        ;cout<<"Your integer is not a prime number"<<n<<endl;
    }
    return 0;
} // end of main.
closed with the note: answered

6 Answers

0 votes
answered Feb 21, 2022 by chinmay gandhi (200 points)

changes in lines number 15, 23, 26

syntax wrong in for (line 15)

extra ';'  (line 23, 26)

this code works:- https://onlinegdb.com/dpTuHD48v

0 votes
answered Feb 21, 2022 by Peter Minarik (86,040 points)

The syntax of the for directive correctly:

for ([initialization]; [condition]; [increment])
{
    [body]
}

Notice that you need semicolons (;) not commas (,) to separate the initialization, condition and increment parts.

Notes

  1. Semicolons typically are placed at the end of the line to terminate the command. While the compiler does not care about white spaces, you really confuse other human beings if the semicolons are not placed right after the command, but at the beginning of the next line.
  2. Your program does not work for negative numbers. (It accept negative numbers and says all of them are primes.)
0 votes
answered Feb 22, 2022 by kotons (140 points)
You have used the wrong syntax in the 'for' loop. it should be semicolon(;) instead of the comma",". Replace all commas the issue will be resolved.
0 votes
answered Apr 2, 2022 by userdotexe (1,340 points)

I adjusted your code, it should work find now:


#include <iostream>
using namespace std;

int main()
{
    int n=0, i=2;
    bool isPrime=true; // Semicolons don't go before statements, but after
    
    cout<<"Enter the integer:";
    cin>>n;
    
    if (n==0 || n==1) {
        isPrime=false;
    }
    else {
        for (i=2; i<=n/2; ++i) { // Use ; instead of ,
            if (n%i==0) {
                isPrime=false;
                break;
            }
        }
    }
    if (isPrime) {
        cout<<"Your integer is a prime number."<<n<<endl;
    }
    else {
        cout<<"Your integer is not a prime number"<<n<<endl;
    }
    return 0;
} // end of main.

:)

0 votes
answered Apr 3, 2022 by Jesse677 (240 points)
https://www.programiz.com/cpp-programming/examples/prime-number
#include <iostream>
using namespace std;

int main() {

  int i, n;
  bool is_prime = true;

  cout << "Enter a positive integer: ";
  cin >> n;

  // 0 and 1 are not prime numbers
  if (n == 0 || n == 1) {
    is_prime = false;
  }

  // loop to check if n is prime
  for (i = 2; i <= n/2; ++i) {
    if (n % i == 0) {
      is_prime = false;
      break;
    }
  }

  if (is_prime)
    cout << n << " is a prime number";
  else
    cout << n << " is not a prime number";

  return 0;
}
0 votes
answered Apr 5, 2022 by Jesse677 (240 points)
edited Apr 6, 2022 by Jesse677
#include <iostream>
using namespace std;

int main()
{
    int number;
    cout << "Enter a number ";
    cin >> number;
    int factors = 0;
    int x = number;
    do {
        if (number % x == 0) {
            factors += 1;
            --x;
        }
        else {
            --x;
        }
    }
    while (x != 0);
    if (factors == 2) {
        cout << number << " is a prime number";    
    }
    else {
        cout << number << " is not a prime number";
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...