How to check if a number is even or odd?

You can check if a number is even or odd using the modulo operator (%). Here are examples in different programming languages:

C++

#include <iostream>
using namespace std;

int main() {
    int number;
    cout << "Enter a number: ";
    cin >> number;

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

    return 0;
}

JavaScript

const isEven = (num) => num % 2 === 0;

const number = 3; // Example number
console.log(isEven(number) ? "The number is Even." : "The number is Odd.");

C

#include <stdio.h>

int main() {
    int number;
    printf("Enter a number: ");
    scanf("%d", &number);

    if (number % 2 == 0) {
        printf("The number is Even.\n");
    } else {
        printf("The number is Odd.\n");
    }

    return 0;
}

You can use any of these examples to check if a number is even or odd. Just input the number you want to check.

0 Comments

no data
Be the first to share your comment!