+3 votes

#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.

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

0 votes

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

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.

- 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.
- Your program does not work for negative numbers. (It accept negative numbers and says all of them are primes.)

0 votes

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

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

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

#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";

}

}

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";

}

}

...