#include <iostream>
bool isPrime(int x) {
if (x == 2) // if user entered 2, the digit is prime
return true;
else if (x == 3) // if user entered 3, the digit is prime
return true;
else if (x == 5) // if user entered 5, the digit is prime
return true;
else if (x == 7) // if user entered 7, the digit is prime
return true;
return false; // if the user did not enter 2, 3, 5, 7, the digit must not be
// prime
}
int main() {
std::cout << "Enter a number 0 through 9: ";
int x{};
std::cin >> x;
if (isPrime(x))
std::cout << "The digit is prime\n";
else
std::cout << "The digit is not prime\n";
return 0;
}
#if statement
33 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
I dont understand how
else in main and
.return false in the top bool function
Communicate
isPrime(number) results in false
If the condition in an if statement is false, it executes the else branch
#include <iostream>
bool isPrime(int x) {
if (x == 2) // if user entered 2, the digit is prime
return true;
else if (x == 3) // if user entered 3, the digit is prime
return true;
else if (x == 5) // if user entered 5, the digit is prime
return true;
else if (x == 7) // if user entered 7, the digit is prime
return true;
else
return false;
}
int main() {
std::cout << "Enter a number 0 through 9: ";
int x{};
std::cin >> x;
if (isPrime(x))
std::cout << "The digit is prime\n";
else
std::cout << "The digit is not prime\n";
return 0;
}
;compile
Program Output
Enter a number 0 through 9: The digit is not prime
Compiler Output
<source>: In function 'bool isPrime(int)':
<source>:13:1: warning: control reaches end of non-void function [-Wreturn-type]
13 | }
| ^
Oppenheimer#2437 | 71ms | c++ | x86-64 gcc 12.2 | godbolt.org
;compile
Program Output
Enter a number 0 through 9: The digit is not prime
Oppenheimer#2437 | 53ms | c++ | x86-64 gcc 12.2 | godbolt.org
@west urchin i removed return false.
Then if you don't input 2 3 5 or 7, then the result of isPrime could be anything
;compile
Program Output
Enter a number 0 through 9: The digit is not prime
Oppenheimer#2437 | 100ms | c++ | x86-64 gcc 12.2 | godbolt.org
@west urchin check it works i put an else statement and wrote return false to make it make sense
;compile | 3
Program Output
Enter a number 0 through 9: The digit is prime
Paweł Syska#4065 | 71ms | c++ | x86-64 gcc 12.2 | godbolt.org

Whats up?
I forgot how to pass input using the compiler bot
Looks good
We on the same page for once
@west urchin can you please address my addition for the else statement in the book function
The else that you added to isPrime? Its unnecessary
Why??
Because it's the same as not having it. The only way to end up at the end is for all the if and else ifs to be false
Yeah exactly I love those words let me say who is gonna know whats at the end
Thts exactly what I'm asking
@jolly cargo you there?
Yes
Yeah bout this
What about it?
Yes I agree but who calls the end who knows whats at the end?@jolly cargo
This question is being automatically marked as stale.
If your question has been answered, run !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.
!solved