Kotlin Program to Print an Integer Entered by the User

In this program, we will learn to code the Kotlin Program to Print an Integer Entered by the User. Let’s understand How to print an integer entered by the user in Kotlin Programming Language.

Taking input in a Programming language is a very important part and every new programmer should learn the concept of taking input from the user.

Now, let’s get straight into the code of the Kotlin Program to Print an Integer Entered by the User.

Kotlin Program to Print an Integer Entered by the User

// Kotlin Program to Print an Integer Entered by the User

import java.util.Scanner;

fun main(args: Array<String>) {
    
    var sc = Scanner(System.`in`);
    
    println("Enter an Integer : ");
    var num = sc.nextInt();
    
    println("The number is "+num);
}

Output

Enter an Integer : 7
The number is 7

Output

Enter an Integer : 18
The number is 18

How Does This Program Work ?

    var sc = Scanner(System.`in`);
    
    println("Enter an Integer : ");
    var num = sc.nextInt();

In this program, we take the input of the integer numbers from the user, using the Scanner class in Kotlin. We import the Scanner Class using import java.util.Scanner;

We use the nextInt() method of the Scanner class to take the input of the integer.

   println("The number is "+num);

Then, we print the number or Integer using the println() function in Kotlin.

This is the Kotlin Program to Print an Integer Entered by the User.

Conclusion

I hope after going through this post, you understand Kotlin Program to Print an Integer Entered by the User.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read: Kotlin Program to Add Two Numbers

Leave a Comment

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