check even or odd

Check Even or Odd Number

Problem Statement

Write a program to check whether a given number is Even or Odd.

A number is:

  • Even if it is divisible by 2

  • Odd if it is not divisible by 2

This is one of the most common beginner and interview questions.

Explanation

To determine whether a number is even or odd:

  • Use the modulus operator (%)

  • If number % 2 == 0, the number is Even

  • Otherwise, the number is Odd

Example

If the input number is 10:

10 % 2 = 0 → Even
 

If the input number is 7:

7 % 2 = 1 → Odd
 

🔍 Approach

  1. Read an integer N

  2. Check N % 2

  3. Print Even or Odd based on the result

Check even or odd logic

#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);

    if(n % 2 == 0)
        printf("Even");
    else
        printf("Odd");

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;

    if(n % 2 == 0)
        cout << "Even";
    else
        cout << "Odd";

    return 0;
}

import java.util.Scanner;

public class EvenOdd {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        if(n % 2 == 0)
            System.out.print("Even");
        else
            System.out.print("Odd");
    }
}

n = int(input())

if n % 2 == 0:
    print("Even")
else:
    print("Odd")

Input


10

Output


Even

⏱️ Time and Space Complexity

  • Time Complexity: O(1)

  • Space Complexity: O(1)

💡 Tips for Beginners

  • % operator is used to find remainder

  • Even numbers always have remainder 0 when divided by 2

  • This logic works for positive and negative numbers

  • Try extending this program to check divisibility by 3 or 5

Conclusion

The Check Even or Odd program is a fundamental problem that helps beginners understand conditions, operators, and decision making. Mastering this logic is essential before moving on to more complex programs involving conditions and loops.

Keep learning with EasyHaiCode 🚀
Happy Coding 😊

0

Subtotal