Software Development Exam  >  Software Development Notes  >  Basics of C++  >  Check Odd/Even Number

Check Odd/Even Number | Basics of C++ - Software Development PDF Download

C++ is a popular programming language used for developing various types of software applications. It is a general-purpose programming language and widely used in various domains such as gaming, mobile applications, desktop applications, and more. In this article, we will explore how to check whether a given number is even or odd in C++.

Checking Even or Odd Numbers

In C++, we can use the modulo operator (%) to check whether a given number is even or odd. The modulo operator returns the remainder of dividing two numbers. If a number is even, then its remainder when divided by 2 is 0, and if it is odd, then its remainder when divided by 2 is 1. We can leverage this property to determine whether a given number is even or odd.

Example Code:

Here is an example code snippet that demonstrates how to check whether a given number is even or odd in C++.

#include <iostream>

using namespace std;

int main() {

  int num;

  cout << "Enter a number: ";

  cin >> num;

  if (num % 2 == 0) {

    cout << num << " is even" << endl;

  } else {

    cout << num << " is odd" << endl;

  }

  return 0;

}

  • In the above code, we first declare an integer variable num to store the input number. We then prompt the user to enter a number using the cout and cin statements. After that, we use an if statement to check whether the given number is even or odd. 
  • If the remainder of the number when divided by 2 is 0, then the number is even, and we print a message indicating that the number is even. Otherwise, if the remainder is 1, then the number is odd, and we print a message indicating that the number is odd.

Example Output
Here is an example output for the above code when the input is 7.

Enter a number: 7

7 is odd

Conclusion

In conclusion, we can use the modulo operator in C++ to check whether a given number is even or odd. This is a simple and effective way to determine the parity of a number. We hope this article has helped you understand how to check even or odd numbers in C++.

The document Check Odd/Even Number | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
70 videos|45 docs|15 tests

Top Courses for Software Development

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

shortcuts and tricks

,

Important questions

,

Previous Year Questions with Solutions

,

Check Odd/Even Number | Basics of C++ - Software Development

,

Extra Questions

,

study material

,

past year papers

,

MCQs

,

Summary

,

practice quizzes

,

Objective type Questions

,

mock tests for examination

,

pdf

,

Free

,

Semester Notes

,

ppt

,

Viva Questions

,

video lectures

,

Check Odd/Even Number | Basics of C++ - Software Development

,

Check Odd/Even Number | Basics of C++ - Software Development

,

Exam

,

Sample Paper

;