Kotlin Program to Check Whether a Number is Even or Odd

In this program, we will learn to code the Kotlin Program to Check Whether a Number is Even or Odd. Let’s understand more about even and odd numbers then we will see the logic to check whether a number is even or odd in Kotlin Programming Language.

Even Numbers

A whole number that can be divided by 2 into two whole numbers is called an even number. For example, 2, 4, 6, 8, 10, etc.

Odd Numbers

A whole number that cannot be divided by 2 into two whole numbers is called an even number. For example, 1, 3, 5, 7, 9, etc.

Now, let’s see the Kotlin Program to Check Whether a Number is Even or Odd.

Kotlin Program to Check Whether a Number is Even or Odd

//Kotlin Program to Check Whether a Number is Even or Odd
import java.util.Scanner;

fun main() {
    var sc = Scanner(System.`in`);
    println("Enter a number: ");
    var num = sc.nextInt();
    
    if(num % 2 == 0){
    println("$num is an Even Number");
    }
    
    else{
         println("$num is an Odd Number");
    }
}

Output

Enter a number: 77
77 is an Odd Number
Enter a number: 18 
18 is an Even Number

How Does This Program Work?

    var sc = Scanner(System.`in`);
    println("Enter a number ");
    var num = sc.nextInt();

In this program, first, we have taken the input of the number from the user, using the Scanner Class of Java. We import the Scanner Class using import java.util.Scanner;

    if(num % 2 == 0){
    println("$num is an Even Number");
    }
    
    else{
         println("$num is an Odd Number");
    }

Then, we have used the if-else to check whether the number is fully divisible by 2 or not.
If the remainder is 0 when the given number is divided by 2 then it is an Even Number otherwise it is an Odd Number. So, to check the remainder we use the modulo operator i.e. “%”.

This is the Kotlin Program to Check Whether a Number is Even or Odd.

Conclusion

I hope after going through this post, you understand how to code Kotlin Program to Check Whether a Number is Even or Odd.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Learn More:

Leave a Comment

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