Creating a simple Kotlin project

It's easy to create a Kotlin-JVM project using IntelliJ IDEA by observing the following steps:

  1. Click on File NewProject.
  2. In the New Project window, select Kotlin/JVM, and choose Next. This is shown in the following screenshot:

  1. In the next wizard, specify the Project name and the Project location. Choose JDK and Kotlin runtime and click on Finish.
  1. In the modules settings, click on Facets and add Kotlin to the module.
  2. We have now created a simple Kotlin project. We can write the code here in Kotlin:

     

  1. Under the source (src) directory, we can create the Kotlin files:

 

Now let's write a simple program to find the factorial of a number. Consider the following Factorial.kt file:

fun main(args: Array<String>) {
val number = 5
var factorial: Int = 1
for (i in 1..number) {
factorial *= i
}
println("Factorial of $number = $factorial")
}

The output is as follows:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset