C++ Program to Check if a Number is Even

«
»

This is a C++ Program to Check if a Number is Even.

Problem Description

The program checks if an entered number is even or not.

Problem Solution

1. The program takes a number and stores it.
2. If the entered number is divisible by 2, then it is even.
3. Else the number is not even.
4. The result is printed.
5. Exit.

advertisement
C++ Program/Source code

Here is the source code of C++ Program to Check if a Number is Even. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int num;
  6.     cout << "Enter the number to be checked : ";
  7.     cin >> num;
  8.     if (num % 2 == 0)
  9.         cout << num << " is Even.";
  10.     else
  11.         cout << num << " is Not Even ";
  12.     return 0;
  13. }
Program Explanation

1. The user is asked to enter a number and its value is stored in the variable ‘num’.
2. If num modulus 2 is equal to 0, which means it is divisible by 2 then num is an even number.
3. Else num is not an even number.
4. The result is then printed.

advertisement
Runtime Test Cases
Case 1 : 
Enter the number to be checked : 186
186 is Even.
 
Case 2 :
Enter the number to be checked : 248601
248601 is Not Even
 
Case 3 :
Enter the number to be checked : 13
13 is Not Even

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

advertisement

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn