EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  C++ for EmSAT Achieve  >  Check Odd/Even Number

Check Odd/Even Number | C++ for EmSAT Achieve 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 | C++ for EmSAT Achieve is a part of the EmSAT Achieve Course C++ for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
70 videos|45 docs|15 tests

Top Courses for EmSAT Achieve

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

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

Extra Questions

,

MCQs

,

Objective type Questions

,

video lectures

,

Previous Year Questions with Solutions

,

Check Odd/Even Number | C++ for EmSAT Achieve

,

ppt

,

Semester Notes

,

Important questions

,

Check Odd/Even Number | C++ for EmSAT Achieve

,

Viva Questions

,

study material

,

Sample Paper

,

Summary

,

pdf

,

Exam

,

Free

,

practice quizzes

,

shortcuts and tricks

,

Check Odd/Even Number | C++ for EmSAT Achieve

,

past year papers

,

mock tests for examination

;