0% found this document useful (0 votes)
12 views7 pages

Practice Code

The document contains a series of Kotlin programming tasks that cover various fundamental concepts such as user input, conditional statements, loops, functions, arrays, classes, inheritance, collections, and lambda expressions. Each task is presented with a code snippet demonstrating the specific concept. The tasks are designed to help learners understand and practice basic programming skills in Kotlin.

Uploaded by

Umar Hayatudeen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Practice Code

The document contains a series of Kotlin programming tasks that cover various fundamental concepts such as user input, conditional statements, loops, functions, arrays, classes, inheritance, collections, and lambda expressions. Each task is presented with a code snippet demonstrating the specific concept. The tasks are designed to help learners understand and practice basic programming skills in Kotlin.

Uploaded by

Umar Hayatudeen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

TASK 1:

fun main() {

println("Hello, Jane!")

Screenshot

Task 2: User Input

fun main() {
print("Enter your name: ")
val name = readLine()
print("Enter your age: ")
val age = readLine()?.toIntOrNull()

if (name != null && age != null) {


println("Hello $name, you are $age years old!")
} else {
println("Invalid input.")
}
}
Task 3: Conditional Statements
fun main() {
print("Enter a number: ")
val number = readLine()?.toIntOrNull()

if (number != null) {
if (number % 2 == 0) {
println("$number is an even number.")
} else {
println("$number is an odd number.")
}
} else {
println("Please enter a valid number.")
}
}

Task 4: Loops and Ranges


fun main() {
println("Numbers from 1 to 10:")
for (i in 1..10) {
println(i)
}

println("\nEven numbers from 1 to 20:")


for (i in 1..20) {
if (i % 2 == 0) {
println(i)
}
}
}

Task 5: Functions
fun sum(a: Int, b: Int): Int {
return a + b
}
fun main() {
println("Sum of 5 and 3 is: ${sum(5, 3)}")
println("Sum of 10 and 20 is: ${sum(10, 20)}")
}

Task 6: Arrays
fun main() {
val names = arrayOf("Alice", "Bob", "Charlie", "Diana", "Eve")

for (name in names) {


println("Hello, $name")
}
}
Task 7: Classes and Objects
class Car(val brand: String, val model: String, val year: Int) {
fun displayDetails() {
println("Car: $brand $model ($year)")
}
}

fun main() {
val car1 = Car("Toyota", "Corolla", 2020)
val car2 = Car("Honda", "Civic", 2022)

car1.displayDetails()
car2.displayDetails()
}

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

class Student(name: String, age: Int, val grade: String) : Person(name,


age) {
override fun displayInfo() {
super.displayInfo()
println("Grade: $grade")
}
}

fun main() {
val student = Student("Jane", 21, "A")
student.displayInfo()
}

Task 9: Collections and Map


fun main() {
val scores = mapOf("Alice" to 85, "Bob" to 67, "Charlie" to 90,
"Diana" to 72)

println("Students who scored above 70:")


for ((name, score) in scores) {
if (score > 70) {
println("$name: $score")
}
}
}
Task 10: Lambda Expression
fun main() {
val multiply: (Int, Int) -> Int = { a, b -> a * b }
println("Product of 4 and 5 is: ${multiply(4, 5)}")
}

You might also like