Search

Admin

2.6 — Boolean values

The next data type we’re going to look at is the boolean data type. Boolean variables only have two possible values: true (1) and false (0).

To declare a boolean variable, we use the keyword bool.

When assigning values to boolean variables, we use the keywords true and false.

Just as the unary minus operator (-) can be used to make an integer negative, the logical NOT operator (!) can be used to flip a boolean value from true to false, or false to true:

When boolean values are evaluated, they actually don’t evaluate to true or false. They evaluate to the integers 0 (false) or 1 (true). Consequently, when we print their values with cout, it prints 0 for false, and 1 for true:

Outputs:

1
0
0
1

If you want cout to print “true” or “false” instead of 0 or 1, you can use std::boolalpha in the iomanip header:

This prints:

1
0
true
false

A first look at booleans in if-statements

One of the most common uses for boolean variables is inside if statements. If statements take the following form:

if (condition) statement

or

if (condition) statement
else statement2;

In both forms, condition is evaluated. If condition evaluates to true (non-zero), then statement is executed. In the second form, if condition evaluates to false, then statement2 is executed instead.

prints:

b is true

Boolean values are also useful as the return values for functions that check whether something is true or not. Such functions are typically named starting with the word is (e.g. isEqual) or has (e.g. hasCommonFactor).

In the following example, we use the equality operator (==) to test if two values are equal. Operator== returns true if the operands are equal, and false if they are not.

Note that in C++, a single equals (=) is the assignment operator, whereas a double equals (==) is a comparison operator to test for equality.

In this case, because we only use equal in one place, there’s really no need to assign it to a variable. We could do this instead:

Boolean variables are quite refreshing in their simplicity!

One additional note: when converting integers to booleans, the integer zero resolves to boolean false, whereas non-zero integers all resolve to true.

Quiz

1) A prime number is a whole number greater than 1 that can only be divided evenly by 1 and itself. Write a program that asks the user to enter a single digit integer. If the user enters a single digit that is prime (2, 3, 5, or 7), print “The digit is prime”. Otherwise, print “The digit is not prime”.

Hint: Use if statements to compare the number the user entered to the prime numbers. Use a boolean to keep track of whether the user entered a prime number or not.

Quiz answers

1) Show Solution

2.7 -- Chars
Index
2.5 -- Floating point numbers

83 comments to 2.6 — Boolean values

  • some guy

    I’m confused as to why you make the IsEqual function a bool, wouldn’t making it an int function work just as well? Or is it declared as a bool function so you have an idea of what the return values will be and what the function does? Sorry if I sound like a newbie :p

    • IsEqual function isn’t a bool, it returns a bool. Boolean values are used to represent values that can only be true or false. When we’re talking about whether a value is equal, either it is (true), or it isn’t (false). Consequently, it makes more sense to return a bool than an int. While technically returning an int would work (returning the value 1 if the parameters are equal, and 0 otherwise), returning a bool is more intuitive and leaves less room for errors to be made.

  • programmer

    I have problem actually I don’t know how to use the boolean with ( cin ) ..

    like this example:

    if i want to ask some one about his car ?

    ===========
    bool car;

    =====
    is the above good solution or not ??

    i’m witing for your answer

    thank you so much

    • When trying to cin boolean values, if the user enters the number 0, this will be treated as the value false. If the user enters anything else, it will be treated as the value true.

      There are several ways to proceed:
      * Tell the user to enter 0 for no and 1 for yes and then cin a bool.
      * Tell the user to enter ‘y’ or ‘n’ and then cin a character.

      Once you get to the lesson on strings, you could also do this:
      * Tell the user to enter “yes” or “no” and then cin a string.

  • another_programmer

    Yup, I had trouble with this one quite a while ago, one thing you’d have to remember is that bool is based on being simply on or off, where if something exists, it’s on, and if it doesn’t it’s off. 1 or 0.

    Boolean values are more ideally used if preexisting values exist much like radio buttons, in HTML for example…

    <form>
    Is your car new or not?<br>
    <input name="newCar" type="radio" value="1" /> Yes<br>
    <input name="newCar" type="radio" value="0" /> No<br>
    </form>

  • Kiran

    Please correct me if I am wrong:
    If the Value of TRUE is 1 (00000001), Doesn’t (!TRUE) make the value to be equal to (111111110) internally? If that is the case then evaluating:
    if (!TRUE) will always be TRUE.

    • Ashish Gupta

      Hi,

      Actually what u r doing is not correct. u r performing bit wise not that is ~ operator in c.
      now u r using operator ! which actually ORs all the bits of the value and then compliments it i.e.
      if TRUE is 1(00000001) then ORing will give its value 1(binary not 00000001) this is only a bit so now it will compliment it to 0 and then return the resulting false(000000000).

      For futher query you can ask me..on new_programers@yahoo.com
      Thnks,

  • Spock

    Hey Alex,

    Nice tutorial section! Some comments that might improve readability of this section:

    1. Mention that the test x==y will output a 0 if false and a 1 if true and discuss the use of this equality as a logic ‘test’. It took me a few moments to appreciate that for myself.

    2. Logic commands have not been discussed in the tutorial yet (if, not etc) so someone new to programming might find this confusing.

    You could also mention that if(1) runs the first command in an if statement and if(0) runs the ‘else’ statement. Interestingly if(5), if(11) and all if(!=0) has the same effect as if(1). i.e all non-zero values are true in the context of the if statement.

    :)

    • Thanks for your thoughts. I added a comment about conversion of integers to booleans because that’s not very intuitive to new programmers. Your other comments are valid, but are discussed in future chapters when I tackle those specific topics.

  • Noha

    Hey!
    In which cases do we use the implicit assignment?

    • You can use it in any case where you’re creating a variable and initializing it with a value. It’s neither better nor worse than explicit assignment as far as I know.

  • someone

    Please i need some help…
    Can please anyone review this code? It is supossed to accept the input of 2 numbers, the asks for the answer if they are equal and not equal and says if the answer is either right or not:

    Thanks!

  • joel

    Great Tutorials, well written and structured. many thanks.

    coming from a background in Oracle and using mainly PL/SQL i have gotten used to the idea that booleans are tristate (true, false or null/unknown) and so an if statement against an uninitialsed boolean var would always return null (and thereby only be handled by “else”) without any warnings - dangerous.

    It might be worth noting for people like me that uninitialised boolean variables, aside from raising a warning at compile and a “break” at runtime (in VC2008 at least), will almost always return true (i assume this because the uninitialised memory address will almost always contain a non-zero value as below).

  • Sam

    I don’t get how it can run the function bool IsEqual(int x, int y), if there is no function call in the program later. I would figure you would need,

    bool Isequal(x,y); <---in main function to call the IsEqual function?

    for the program to read

    bool IsEqual(int x, int y)
    07.{
    08. return (x == y); // use equality operator to test if equal
    09.}

    so how does it work without a function call being made?

    • Collin

      the if statement calls the function to check if (x==y)

  • Alex

    Could someone please give me a definitive answer about the difference between explicit and implicit assignments.

    Thanks

    • Daniel

      I’ve never seen that type of assignment used for a built-in, although I do not doubt that it works. I believe that “int i = 1;” uses int’s default constructor THEN uses int’s assignment operator. Whereas “int j(1);” uses a version of int’s constructor which accepts an integer. In theory the second should be more efficient although compilers are most likely smart enough to optimise away the extra step.

  • Stephan

    Hi Alex (Admin)!

    Great site.
    Just wanted to say that I find your site extremely educational, and for me, easy to follow. Good work!

    P.S.
    I get just as much fun/learning reading the comments at the end of the tutorial as I do from the lessons. I can’t finish a section without reading the comments!

    Brilliant!!!

  • Mac

    Hi, i tried to make this program work with three numbers:

    i.e.
    return (x == y == z)

    the program compiled fine and ran, but did only gave the result for “else” (false)

    after i took away “int z” it worked just fine…

    does bool only work for two variables?

    • The reason is when you say a == b == c, first it checks a and b and if those two numbers are equal, you are trying to check if c is equal to 1(true). Try this:

      bool IsEqual(int a, int b, int c)
      {
      if(a == b)
      {
      if(b == c)
      return true;
      }
      else
      return false;
      }

  • Guntis

    Yay, I think I completely understand how bool works now.
    Thanks Alex, this is so much better than even some of those books on cpp.

    P.S. Here’s my little test that succeeded btw.

  • joha

    wouldnt it be the same to put instead of x==y, this: x=y????
    i tried and got the same result so…..what is the difference and when do I use each operator????

    Thanks..

  • gurbux

    hey nice explanation alex very helpfull………..

  • xero

    I want to ask why following code works?

    #include
    int main()
    {
    using namespace std;
    int x;
    cout << "Please feed in the first number" <> x;
    int y;
    cout << "Please feed in the second number" <> y;
    bool z;
    if (x==y)
    z = true;
    else
    z = false;
    if (z)
    cout << "Both digits are equal" << endl;
    else
    cout << "Both digits are different" << endl;
    return 0;
    }

    When the user feeds in two different numbers then the statement which says
    if (z)
    must hold 0 as z and in return should type "Both digits are equal" because this cout statement follows if (z) statement.
    Please comment…

  • John

    Hi!

    So I’m getting the coding right when trying some things, I’m just struggling to understand something. Line 27 of your example to be exact.

    On line 26 we declare the variable beEqual. Then we have a function call to IsEqual in order to initialize beEqual. Am I right in assuming that IsEqual will return to the caller function a 1 if both values are equal, and a 0 if they are not equal?

    Now line 27. We say: if (beEqual)… what exactly does this mean? Because I’m sure beEqual=beEqual, so why dont we always print “x is equal to y” ?

    Or should I read it as: if (1) and if (0)?

    I hope I’m making sense here!

    • Alex

      isEqual() will return true (1) if the values are equal, and 0 otherwise.

      “if (beEqual) …” is the same as writing “if (beEqual==true) …”. So in this case, we’re testing beEqual to see if it’s true, and which statement is executed depends on whether that equality test is true or false. I’ll make this more clear in the lesson text.

  • Carol

    Can any one help check my code? There’s some semantic error, but I can’t figure out why.

    #include <iostream>
    using namespace std;

    bool isPrime(int)
    {
        int x;
        if (x==2)
            return true;
        else if (x==3)
            return true;
        else if (x==5)
            return true;
        else if (x==7)
            return true;
        else
            return false;
    }

    int main()
    {
        cout << "Please enter a 1-digit number." << endl;
        int myNumber;
        cin >> myNumber;
        
        if (isPrime(myNumber))
            cout << "The digit is prime" << endl;
        else
            cout << "The digit is not prime" << endl;
            
        return 0;
    }

    • Alex

      Change:

      to:

      As you wrote it, you didn’t give your function parameter a name, so the value being passed in (myNumber) is being discarded. You defined variable x inside the function, but you never initialized it with a value, so it’s essentially useless.

  • Daniel Ricci

    One additional note: when converting integers to booleans, the integer zero resolves to boolean false, whereas non-zero integers all resolve to true.

    At this point, you should just specify that any numerical value that is not 0 or 0.0 w.r.t its data type will be true.

  • Tito

    I just wanted to share my quiz answer for any future readers.

    • Matthew

      Thank you for this!  I was trying to figure out how to use bool prime(false) or bool prime(int x) as a forward declaration and could not figure out what I was doing wrong.  Now my compiler errors make sense.

  • Melissa Arredondo

    Hi I have an assignment at school similar to this but I’m having a hard time with it. Hopefully someone can help! So the input can be from 2-100 and then determine whether it is prime or not. Then if it is not prime, I need to be able to print whether it is divisible by 2, 3, 5, or 7. How do I do that?

  • Snowman

    I think I took the quiz too seriously =P

    PS. No, I didn’t read the hint, it’s cheating =D

  • Rich S.

    I’m not sure if I understand the significance between = and ==. I get that = is assigning a value while == is comparing, but it seems like replacing the == with = in the sample code should either give a syntax error or the correct result. When I try this, it compiles correctly, but entering an 8 gives me the result "This digit is prime." I would appreciate it if someone could help me to understand why this is so.

    • Alex

      It sounds like you’re doing something like this:

      So first, this will assign the value 2 to x, which probably isn’t what you want.

      But the real question here is what does x = 2 evaluate to? The answer, it turns out, is x.

      So after x = 2 evaluates, you’re left with:

      which will be true if x is anything other than 0. So the … part of the code is executing when you aren’t expecting it to, and this is setting the value of isPrime to true.

    • cpplx

      seems like some people have a doubt about operators represented by using common symbols.
      maybe if you extend "1.5 — A first look at operators" with that = and == (for example) should be interpreted as "assign" and "compare" NOT as "equals" and "equals equals".
      also that == or <= should be interpreted as a single symbol, not 2 keys pressed one after the other, but as if it is 1 key pressed.
      make a remark that due the lack of symbols available in the keyboards, the language uses a combination of what is available to represent… stuff.
      some educators like to present programmers as being lazy(which might be true for the majority of them) hence the reason to write "==" instead of "compare".
      you get the idea. use better words :)

  • programmer, another one

    in the above solution, couldn’t you have put the

        if (prime)
            cout << "The digit is prime" << endl;
        else
            cout << "The digit is not prime" << endl;

    part inside the other if statements like you would do in most other languages?

    for example:

        if (x == 2)
            prime = true;
            std<<cout::"the digit is true";
        else if (x == 3)
            prime = true;
            std<<cout::"the digit is true";
        else if (x == 5)
            prime = true;
            std<<cout::"the digit is true";
        else if (x == 7)
            prime = true;
            std<<cout::"the digit is true";

    I’ve also noticed that c++ does not automatically edit the indentation in the statements like compilers in most other languages would.

    Thanks for the reply, and for building this amazing website

    • Alex

      You could, but I wouldn’t recommend doing it this way. Why? Because if you want to change what is printed, you have to change 4 statements instead of one. Although it’s not that bad to do with 4 statements, imagine if there were 100, or 1000.

      One of the goals when programming should be to reduce redundancy, and your code adds redundancy.

  • Suggestion for this part, another common prefix for bool functions is has. And since you do a lot to ensure good coding style (love that you do that btw.) I would add that it’s not only common but in most cases considered good style, since it for most boolean purposes makes the purpose of the function clearer.

  • Brian

    Hi,
    I’m using Visual studio 2010.I believe my code is similar to the solution above(in the quiz)However,my program seems to recognize all inputs from user as being prime including numbers that are not prime. How do I solve this?

    • Alex

      Reply to this message and post your code so we can see what’s wrong.

      • Brian

        • shakey2

          line 12:

          should be

          You want Prime to default to being false because you are only making it true if it matches one of the conditional statements(if (x == 2) for example). If it doesn’t match one of those statements then those statements get ignored and Prime stays false so when the program gets to

          then it will skip to the else statement(telling the user their number is not prime) because its not true.

  • Vishal

    Hello Alex,

    First of all, thanks for designing such a great tutorial! It makes learning a lot of fun since the approach is very systematic; not to mention the fact that it uses language that new programmers are comfortable with.

    I have a doubt regarding the following program (one of the examples in this lesson)-

    Why is it ‘if (equal)’ ?
    Why not ‘if (equal == 1)’ or ‘if (equal == true)’ ?
    I checked all three variations and they all work perfectly, so what’s the difference?

    Besides, couldn’t we just write the program as-

    It makes it shorter, and perhaps there’s a lower chance of errors.

    Please enlighten me! 😛
    Thanks!

    • Alex

      As you note, we use “if (equal)” because it evaluates identically to “if (equal == true)”. You can see why this is true by making a table:

      equal equal == true
      true true
      false false

      Therefore the “== true” part is redundant.

  • shakey2

    I came up with a different solution to the one you listed:

    Aside from having less to type is there a reason your solution is better than this?

    • Alex

      Your solution works but has a lot of redundancy. If you ever wanted to update the string “The digit is prime” you’d have to update it in 4 different places.

      For a simple program like this that’s not that big of a deal, but as programs get more complex, the more you can reduce redundancy the better.

  • Tree Stan

    Hi, I recently attempted the quiz of this section and wanted to avoid writing 4 else if statements. I’m not sure where I went wrong:

  • Okay…I am confused. I don’t know whether it is a good question or not. A boolean variable reserves 1 byte in memory but can only hold two possible values, true(1) or false(0). A single binary digit can also hold two values 1 or 0. My question is why a boolean variables takes 8 bit to represent.

    • Alex

      It’s a good question.

      Booleans really only need one bit to represent a true or false state, however as you note, bool variables use a whole byte. Why? Because the smallest unit of memory that a computer architecture can get or set is 1 byte. If booleans were only 1 bit, all of the booleans within the same byte would have the same memory address, and there’s be no way to differentiate them!

      There are ways to pack 8 booleans into a single byte. I cover these some of these techniques in future lessons.

  • In the above program, everything before "bool bothZero(false) ;" works fine. But when I run the whole program and input the value zero (0) for both x and y, the else statement is executed and outputs "But both the numbers were not zero." and when I input a non zero number for either x or y, my program outputs " Both the numbers were zero. " and if the given non-zero numbers are equal, It results "But both the numbers were not zero. " .

    When I do this with my program :

    It works fine with value 0 for both x and y but even when I input non-zero but equal numbers for both x and y, program still outputs the same "Both the numbers were zero. " . What is the problem. Please give a fine version of this program. My program is supposed to take two integer numbers from user. If both the numbers are zero, it should print "Both the numbers were zero. " and if user inputs 0 for either x or y, I want to print "But both the numbers were not zero. " Everything except this part of the program runs fine so I am not including the output given by rest of the program.

    • Alex

      Does not do what you’re assuming it does.

      This evaluates as “if ((x == y) == 0)”, which will only be true if x and y are different numbers, and has nothing to do with whether either is 0.

      The easiest way to fix this is to use the logical AND operator:

      We cover logical AND (and other logical operators) in the next chapter.

  • This works fine…please explain how.

    • Alex

      This isn’t a C++ question -- it’s a logic question, which you should be able to work out for yourself. :)

      • Thank you so much Alex for these tutorials. Now at least I know why my codes above were working and not working. I can see how bad I was coding a few months ago. Probably, I would never do these type of mistakes in my future programs. You made me write better codes and m still learning. Someday if I get a job, I’ll miss your contribution. You are helping me to be a better programmer day by day. You are doing a great job.
        Thanks Alex…

  • Methos

    I came up with the following (main just calls these functions in order) and while I’m feeling a little clever, I thought I’d ask if any of this is a truly terrible idea.

    I called a function from inside itself as a check the user actually put in an appropriate value that unwinds whenever he finally cooperates. Also I used the fact that the prime comparisons are mutually exclusive to macgyver an ‘or’ check on the variable’s primeness (though I think based on what you’ve said I could get away with a sum like this for an ‘or’ so long as the number of conditions is small enough not to run into overflow issues).

    • Alex

      Cool solution! None of these is truly terrible.

      However, I do have a few suggestions for you:

      * Recursion is useful when the solution to a problem can be calculated by using the result from a subset of the problem. That isn’t really the case here. A do-while loop would be more appropriate. This is covered in chapter 5.

      * Instead of using + in primeCheck() (which is clever), use the logical OR operator (||). It’ll make it clearer what your intent is, as well as be more efficient due to short circuit evaluation. This is covered in chapter 3.

      * Give your variables better names. :)

  • radiz

    Hi, can you explain why i can’t use boolean datatype in a loop condition like this, please?

    • Alex

      You can use a boolean datatype in a loop condition like that. However, your program has errors that will prevent it from working correctly:
      * You never assign a value to “input” before you use it in an expression.
      * Your loop variable (condition) is never changed, so your loop will either not execute at all (if condition is false), or go into an infinite loop (if condition is true).

      Remember that variables can only hold values, not expressions.

  • person

    So, if the if statement is true than the else statement after it is always ignored?

    Where is it decided in your solution that if prime is true that the if statement will be chosen?

    • Alex

      Yes, the else is ignored if the if statement condition is true.

      > Where is it decided in your solution that if prime is true that the if statement will be chosen?

      I’m not sure I understand the question.

      This is the code that checks if prime is true, and prints “The digit is prime” if it is.

      • person

        It’s difficult to explain what I mean.

        If prime is false why does it jump past if directly to else?

        What establishes that prime must be true for the if statement to be chosen?

      • person

        I think I understand now.

        Prime can be false but still be a variable called “prime”, so I didn’t see a reason why the if statement couldn’t be satisfied whether prime was true or not because the answer will be the variable “prime” regardless.

        However, I realize that the compiler sees it not as the variable named prime, but rather as a boolean 1 or 0.

        True prime is different from false prime because 1 is not equal to 0.

        • Alex

          Right. prime is a variable that can hold two values: 0 (false) and 1 (true). Our program uses logic to determine whether variable prime is set to true or false.

          We then use an if statement to determine which of two statements executes based on whether we set variable prime to true or false previously.

  • Yujian

    Hello

    I dont understand why u used false instead of true in line 12. Why cant we use true instead of false there?

    are u doing this because of we have to assume that everything is false at the start and then classify those who are not?

    • Alex

      The comments in the quiz answer show the logic behind this choice:
      // We start by assuming the user did not enter a prime number
      // Then we test to see if they did

      If we started with prime = true, then how would prime get set to false if the user didn’t enter a prime number? We _could_ do that with an else statement at the end of the if/else chain, but the logic is slightly messier that way. I like trying to keep the number of if and else statements to a minimum.

      > are u doing this because of we have to assume that everything is false at the start and then classify those who are not?

      We don’t have to do it this way, but it’s easier to enumerate all the cases where prime should be set to true than where it should be set to false.

  • Yannick

    So I tried the quiz and at my first attempt I got the same answer as you do but after that I wanted to find something to not use else if 3 times and I got this, but for some reason I cant build it and I can’t find my mistake.. btw this is the error i get:
    1>---- Build started: Project: primenumberOrNot, Configuration: Debug Win32 ----
    1>  primenumberOrNot.cpp
    1>LINK : fatal error LNK1168: cannot open C:\C++ Projects\primenumberOrNot\Debug\primenumberOrNot.exe for writing
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    • Alex

      It sounds like your executable has either been flagged as read-only or locked for some reason. Is it possible you have an instance of it already running when you’re trying to compile?

      In the worst case, restarting your system will probably resolve this.

  • Mr D

    Thanks, amazing tutorials!

    I’m having a hard time getting my head around the initialization of bool.

    Consider the program i wrote below. As it stands, it outputs the correct answer. However, if i change the first "false" to "true", it outputs the opposite answer! Why does it matter how the bool is initialized? Why doesn’t the compiler believe the statement "if (45 == x) what = true;"?

    • Alex

      If you initialize “what” to true, then how would it ever get set to false if 45 does not equal x? If you’re going to invert the initialization, you also need to invert the if statement that sets “what” to true.

      Also, “what” is a pretty poor name for a variable. A name like “equals” would be easier to read.

  • R00kie

    Alex,

    I am trying to figure out something in the 2 codes you wrote :

    1)

    It is clear that the bool function returns a boolean value (true/false), but why x == y means true ?

    You never defined x == y as being true ? so how does the compiler assigns the "true" value when x == y ?

    The operator == does all the work , and returns "true" ? I ask this because "true" or "false" were never defined in the function …

    I wrote the same code like this , as it makes more sense to a newbie like me . Is my code incorrect or redundant as I see the compiler not complaining  ??

    2)

    The 2nd code is in your Quiz

    Why is "prime" considered true in the "if statement" when the value of true was not assigned in that if statement ?

    I wrote my code like this :

    So in my "if" I specifically assigned the "true" value to "prime"  ? But how does the "if" in your code knows that "prime" is "true" by default ??

    It picks up the value of "prime" from here ?? from x == 7, prime = true ???

    I am trying to figure out the logical aspects of your code, and to understand why the compiler executes your last "if statement" with "prime" being true by default ?

    • Alex

      true and false are keywords in the C++ language, so C++ knows what these two things are without us defining them.

      1) In this code:

      The == operator does all the work. It compares x and y to see if they are equal. If they are equal, it evaluates to true. If they are not equal, it evaluates to false.

      2) It’s not clear to me what you’re actually asking here. Prime is a variable, so it has whatever value was last assigned to it. Also, don’t mix up operator= (assignment) with operator== (equivalence).

      The program works by initially setting prime to false as a default value. Then the program checks to see if x is equal to 2, 3, 5, or 7. If so, prime is set to true. Otherwise, prime still has the value false.

      When we get down to here:

      prime has already either been set to true by the if/else statements above (depending on the value of x), or has been left at the default value of false.

      The program evaluates prime to see what value it has (true or false, as set above) and conditionally executes the appropriate output statement.

  • Gayan

    #include <iostream>

    bool isPrime(int x)
    {

        return (x==2||x==3||x==5||x==7);
    }

    int main()
    {
        using namespace std;
        cout<<"Enter a single digit integer! "<<endl;
        int x=0;
        cin>>x;

        bool Equal=isPrime(x);

        if(Equal)
            cout<<x<<" is a Prime number ! "<<endl;
        else
            cout<<x<<" is not a Prime number ! "<<endl;

        return 0;
    }

    What about this? :)

Leave a Comment

  

  

  

four × 4 =

Put C++ code inside [code][/code] tags to use the syntax highlighter