Write a C++ program to check whether given number is even or odd.

Write a C++ Program to Check Whether a Given Number is Even or Odd

Checking whether a number is even or odd is one of the most basic and important programs in C++. It helps beginners understand conditional statements and operators. In this tutorial by cslearning.org, we will learn how to write a simple and student-friendly C++ program to determine whether a given number is even or odd.

This guide includes introduction, logic, simple C++ source code, sample output, and viva questions with answers.

1. Introduction of Even and Odd Numbers

An even number is a number that is completely divisible by 2. It leaves remainder 0 when divided by 2.

Examples of even numbers: 2, 4, 6, 8, 10

An odd number is a number that is not completely divisible by 2. It leaves remainder 1 when divided by 2.

Examples of odd numbers: 1, 3, 5, 7, 9

In C++, we use the modulus operator % to find the remainder of a division.

2. Logic of the Program

To check whether a number is even or odd, follow these steps:

  1. Start the program.
  2. Declare an integer variable.
  3. Take input from the user.
  4. Use the modulus operator % to divide the number by 2.
  5. If the remainder is 0, the number is even.
  6. Otherwise, the number is odd.
  7. Display the result.

Example:
If the user enters 8
8 % 2 = 0
Since remainder is 0, it is an even number.

If the user enters 7
7 % 2 = 1
Since remainder is not 0, it is an odd number.

3. C++ Source Code (Easy Student Friendly Code)

#include <iostream>
using namespace std;

int main()
{
    int num;

    cout << "Enter a number: ";
    cin >> num;

    if(num % 2 == 0)
    {
        cout << num << " is an Even number.";
    }
    else
    {
        cout << num << " is an Odd number.";
    }

    return 0;
}

Explanation:
#include <iostream> is used for input and output operations.
num % 2 gives the remainder after dividing the number by 2.
If the remainder is 0, the number is even. Otherwise, it is odd.
The if-else statement is used for decision making.

4. Sample Output

Example 1:
Enter a number: 10
10 is an Even number.

Example 2:
Enter a number: 7
7 is an Odd number.

5. Viva Questions with Answers

Q1: What is an even number?
Answer: An even number is a number that is completely divisible by 2 and leaves remainder 0.

Q2: What is an odd number?
Answer: An odd number is a number that is not completely divisible by 2 and leaves remainder 1.

Q3: Which operator is used to find remainder in C++?
Answer: The modulus operator % is used to find the remainder.

Q4: Which statement is used for checking conditions in this program?
Answer: The if-else statement is used to check the condition.

Q5: Can this program work for negative numbers?
Answer: Yes, it can also determine whether negative numbers are even or odd.

This program helps beginners understand conditional statements and operators in C++. Practice more basic programs on cslearning.org to strengthen your programming fundamentals.

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top