Tuesday, April 23, 2024
HomejQuery4 C++ Packages to examine entered quantity is Even or Odd

4 C++ Packages to examine entered quantity is Even or Odd


Examine if a quantity is even or odd in C++ Program

On this brief tutorial collection of C++, we’ll present you find out how to examine whether or not a quantity entered by the person is Even or Odd.

  • The quantity which could be divided by 2 is even
  • The quantity that may’t be divided by 2 is odd

First Program – Utilizing % operator in if else

The % operator is used to return the rest after division. As such, the rest of a quantity that may be divided by two is all the time zero – we are able to use it to attain our process i.e. a quantity enter by the person is even or odd.

See the C++ program under that makes use of the % operator with if..else statements. For taking person enter, we used C++ cin as proven in this system under:



Pattern Output 1:

CPP-even-odd

Pattern Output 2

CPP-even-odd-modulus

Second Method – Utilizing Bitwise operator AND

First, take a look at this C++ program that makes use of AND bitwise operator to examine if the quantity is Even or Odd:



Pattern output 1

CPP-even-odd-bitwise

Pattern output 1

Third Method – Utilizing ternary operator instance

This program additionally tells if the entered quantity is even or odd by utilizing the ternary operator. Take a look on the code and pattern outputs:



Output:

CPP-even-odd-ternary

Fourth Resolution – Utilizing Swap Case Assertion

As such, the swap case can be a decision-making assertion in C++. We embody this instance simply so as to add another choice in the event you get this task.

This instance additionally makes use of the modulus operator %), identical to with the if assertion and the rest choices are used as circumstances.

See the code and pattern output under:



Output:

CPP-even-odd-switch

 


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments