#if statement

33 messages · Page 1 of 1 (latest)

formal summit
#
#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;
}
glad pebbleBOT
#

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.

formal summit
west urchin
#

isPrime(number) results in false
If the condition in an if statement is false, it executes the else branch

formal summit
#
#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;
}
urban saffronBOT
#
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 | }
      | ^
urban saffronBOT
#
Program Output
Enter a number 0 through 9: The digit is not prime
formal summit
west urchin
#

Then if you don't input 2 3 5 or 7, then the result of isPrime could be anything

urban saffronBOT
#
Program Output
Enter a number 0 through 9: The digit is not prime
formal summit
urban saffronBOT
#
Program Output
Enter a number 0 through 9: The digit is prime
shadow remnant
formal summit
shadow remnant
#

I forgot how to pass input using the compiler bot

west urchin
#

Looks good

formal summit
formal summit
jolly cargo
jolly cargo
# formal summit 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

formal summit
formal summit
#

@jolly cargo you there?

jolly cargo
jolly cargo
#

What about it?

formal summit
glad pebbleBOT
#

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.

formal summit
#

!solved