0

I had to create a function that takes an integer parameter called number and the function would check to see if number is a prime number or not. Everything works but the output to the user would be a either 0 or 1, which is false and true, respectively. But how can I change that to instead return "True" or "False" if the number is prime or not, respectively.

Here is my code:

#include<iostream>

int isPrime(int);

int main()
{
  int number = 3;

  std::cout << isPrime(number);

  return 0;
}



int isPrime(int number)
{
  bool isPrime = true;

  if(number < 0)
    std::cout << "Number must be a positive integer.\n";

  else if(number == 0 || number == 1)
    isPrime = false;
  else
  {
    for(int divisor = 2; divisor <= number / 2; divisor++)
    {
      if(number % divisor == 0)
      {
        isPrime = false;
        break;
      }
    }
  }

  
  return isPrime;
}
Matthew
  • 19
  • 3

1 Answers1

0

First of all, your function should return a bool instead of an int:

bool isPrime(int number) ...

Next, you need to tell cout that you want to print boolean values as true or false instead of 1 or 0. This can be done with std::boolalpha:

std::cout << std::boolalpha << isPrime(number);

You have to #include <ios> for that.

Edit

To answer some questions from the comments:

What's the issue if the function is of type int and not bool?

The whole point of a type system is to express and restrict the behavior of a program. If you call a function you better know what you pass into that function and what that function returns. Types help you with that.

Take your isPrime function as an example. Let's say you didn't write that function but someone else did and you just use it as a library. You see the function signature int isPrime(int);. The function takes an int as an argument and you can probably guess what that argument has to be: the number that you want to check.

Now look at the return type int. What does this mean? The function name isPrime indicates that the function answers a simple yes/no question: is the given number a prime number? However, an int can represent more than just yes or no. For example, what does it mean if that function returns 2 or -1000 (remember: you didn't write this function and you cannot look at its implementation)? Which value represents yes and which no? If you change the return type of that function to bool, a type that only has 2 possible values: true or false, you immediately get what the return value means.

do I print that out in the main function

This is more of a design question and always depends on your very specific use case. Given only the code from the question: yes. You separated business logic, the part that does the prime check, from an IO / user interaction part. Generally, this makes code more readable and debuggable, two very important aspects of software development.

Timo
  • 9,269
  • 2
  • 28
  • 58
  • 1
    I always thought `std::boolalpha` is part of `` – therealcain Feb 28 '21 at 16:51
  • @therealcain yeah I thought that as well. Only noticed it when linking to cppreference. – Timo Feb 28 '21 at 16:52
  • @Timo Thank you for pointing that out, I was looking around and I noticed that... But it still worked. What's the issue if the function is of type int and not bool? Also, do I print that out in the main function right? – Matthew Feb 28 '21 at 16:55