Kotlin Program to Check Whether a Number is Even or Odd

Introduction

In programming, one of the fundamental tasks is to determine whether a given number is even or odd. This task can be easily accomplished using the Kotlin programming language. Kotlin, a statically-typed programming language running on the JVM, is concise, expressive, and designed to interoperate fully with Java. This article will guide you through writing a Kotlin Program to Check Whether a Number is Even or Odd. We’ll cover three different solutions, each explained with examples and their respective outputs.

Solution 1: Using Modulus Operator

The simplest way to determine if a number is even or odd is by using the modulus operator %. If the remainder of the division of a number by 2 is 0, the number is even; otherwise, it is odd.

Code Example

Kotlin
fun main() {
    val number = 7
    if (number % 2 == 0) {
        println("$number is even")
    } else {
        println("$number is odd")
    }
}

Output

Kotlin
7 is odd

Explanation of Output

In this example, the number 7 is checked. Since 7 % 2 equals 1, the program prints “7 is odd”.

Solution 2: Using Bitwise AND Operator

Another way to check if a number is even or odd is by using the bitwise AND operator &. The least significant bit (LSB) of an even number is always 0, and for an odd number, it is 1. Thus, performing a bitwise AND operation with 1 (number & 1) will yield 0 for even numbers and 1 for odd numbers.

Code Example

Kotlin
fun main() {
    val number = 12
    if (number and 1 == 0) {
        println("$number is even")
    } else {
        println("$number is odd")
    }
}

Output

Kotlin
12 is even

In this example, the number 12 is checked. Since 12 & 1 equals 0, the program prints “12 is even”.

Solution 3: Using Extension Function

Kotlin allows you to add new functions to existing classes using extension functions. We can create an extension function for the Int class to check if a number is even or odd.

Code Example

Kotlin
fun Int.isEven(): Boolean {
    return this % 2 == 0
}

fun main() {
    val number = 19
    if (number.isEven()) {
        println("$number is even")
    } else {
        println("$number is odd")
    }
}

Output

Kotlin
19 is odd

In this example, an extension function isEven() is created for the Int class. The number 19 is checked using this function. Since 19 % 2 equals 1, the function returns false, and the program prints “19 is odd”.

Conclusion

Determining whether a number is even or odd is a basic yet essential task in programming. Kotlin provides multiple ways to accomplish this task, including using the modulus operator, bitwise operations, and extension functions. Each method has its use case and can be chosen based on the specific requirements of the problem at hand. By understanding and implementing these different solutions, you can leverage Kotlin’s features to write efficient and readable code.

With the examples provided, you should now be able to implement your own Kotlin program to check whether a number is even or odd. Happy coding!