Kotlin Examples

Welcome to The Coding College! In this post, we’ve curated a collection of Kotlin examples to help you learn by doing. These examples cover essential programming concepts and practical applications, making it easier to understand Kotlin’s features and syntax.

1. Hello World in Kotlin

fun main() {
    println("Hello, World!")
}

Output:

Hello, World!

2. Basic Arithmetic Operations

fun main() {
    val num1 = 10
    val num2 = 5

    println("Addition: ${num1 + num2}")
    println("Subtraction: ${num1 - num2}")
    println("Multiplication: ${num1 * num2}")
    println("Division: ${num1 / num2}")
}

Output:

Addition: 15  
Subtraction: 5  
Multiplication: 50  
Division: 2  

3. Using Conditional Statements

fun main() {
    val age = 20

    if (age >= 18) {
        println("You are eligible to vote.")
    } else {
        println("You are not eligible to vote.")
    }
}

Output:

You are eligible to vote.

4. Loops in Kotlin

For Loop Example:

fun main() {
    for (i in 1..5) {
        println("Number: $i")
    }
}

Output:

Number: 1  
Number: 2  
Number: 3  
Number: 4  
Number: 5  

While Loop Example:

fun main() {
    var count = 1

    while (count <= 5) {
        println("Count: $count")
        count++
    }
}

Output:

Count: 1  
Count: 2  
Count: 3  
Count: 4  
Count: 5 

5. Working with Functions

fun greetUser(name: String): String {
    return "Hello, $name!"
}

fun main() {
    println(greetUser("Naman"))
}

Output:

Hello, Naman!

6. Classes and Objects

class Person(val name: String, val age: Int) {
    fun displayInfo() {
        println("Name: $name")
        println("Age: $age")
    }
}

fun main() {
    val person = Person("Naman", 24)
    person.displayInfo()
}

Output:

Name: Naman  
Age: 24  

7. Collections Example

List Example:

fun main() {
    val fruits = listOf("Apple", "Banana", "Cherry")
    for (fruit in fruits) {
        println(fruit)
    }
}

Output:

Apple  
Banana  
Cherry  

Map Example:

fun main() {
    val studentScores = mapOf("John" to 85, "Emma" to 92, "Alex" to 78)
    for ((name, score) in studentScores) {
        println("$name scored $score")
    }
}

Output:

John scored 85  
Emma scored 92  
Alex scored 78  

8. File Handling

import java.io.File

fun main() {
    val fileName = "example.txt"
    val content = "Welcome to Kotlin Programming!"

    // Writing to a file
    File(fileName).writeText(content)

    // Reading from a file
    val fileContent = File(fileName).readText()
    println("File Content: $fileContent")
}

Output:

File Content: Welcome to Kotlin Programming!

Why Learn Kotlin with The Coding College?

We provide concise, practical, and beginner-friendly tutorials to accelerate your learning journey. These examples aim to solidify your understanding of Kotlin programming concepts, making you more confident in writing clean and efficient code.

Conclusion

Kotlin is a versatile language that simplifies many programming tasks. Practicing these examples will help you grasp key concepts and apply them to real-world scenarios. For more Kotlin tutorials and examples, visit The Coding College and take your programming skills to the next level!

Leave a Comment