In this post, you’ll learn how to Check If a Number is Even or Odd in C++.
This lesson, you will learn how to check if a number is odd or even, with a simple mathematical function, decision making statement and relational operator using the C++ language. Let’s look at the below source code.
How to check if a Number is Even or Odd?
RUN CODE SNIPPETSource Code
#include<iostream> using namespace std; int main() { int number, remainder; cin >> number; cout << "Enter the number : "<<number <<endl; remainder = number % 2; if (remainder == 0) cout <<"\n"<< number << " is an even number "; else cout <<"\n"<< number << " is an odd number "; return 0; }
Input
7
Output
Enter the number : 7 7 is an odd number
#include <iostream>
- This line which is called the header file.
#include
statement tells the compiler to use available files and<iostream>
is the name of the specific that we have used in this code. The<iostream>
file stands for Input and Output statement.
using namespace std;
- The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.
int main();
- This line usually controls the function of the code, as it calls the functions to perform their tasks.
- The
int main()
shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.
{ }
- The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
- Every statement and value between these braces belong to the main function.
The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.
- When we divide a number by two and the reminder is 0, then the number is an even number. Using this principle let’s build the source code to find the whether the number is odd or even.
- First declare the variables (number, reminder) as integers.
- Then we collect the number from the user using
cin >> number;
and display it with thecout << "Enter the integer"
, and the Statement must always be written in ” ” followed by<< number
to display it. - With the mathematical function
remainder = number % 2;
we find the reminder. - Using the relational operator
(remainder == 0)
we can find if the number is odd or even. This function checks if both the values are equal, that is the reminder is equal to 0. If it is we can use the decision making statement to display the answer. if (reminder == 0)
then we can display the statement that the number is even if not with theelse
statement we can display that the number is odd.cout <<"\n"<< number << " is an even number ";
we display the number with<< number <<
and then the output statement.- End the function with
return 0;
this returns the function to main( ).
Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line and ‘\n’ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.
Additional Source Code
You can also try the source code given below, the following code is structured to find whether the number is odd or even using ternary operator, which is a shorter version of the decision making operator.
RUN CODE SNIPPET#include<iostream> using namespace std; int main() { int n; cin >> n; cout << "Enter the number : "<<n <<endl; (n % 2 == 0) ? cout <<"\n"<< n << " is an even number " : cout <<"\n"<< n << " is an odd number "; return 0; }
Input
8
Output
Enter the number : 8 8 is an even number
- In this code we use the mathematical function
number % 2
directly in the relational operator(number % 2 == 0)
with the ternary operators? :
instead of theif else
statement. - And here we use the variable n as the integer.
- The rest of the statements is similar to find whether the number is Odd or Even.