Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn kotlin Exercises


Certainly! Here are a few simple Kotlin exercises along with examples:

Exercise 1: Calculate the Area of a Rectangle

Problem: Write a Kotlin program to calculate the area of a rectangle given its length and width.

Example Solution:

fun calculateArea(length: Double, width: Double): Double {
return length * width
}

fun main() {
val length = 5.0
val width = 3.0
val area = calculateArea(length, width)
println("The area of the rectangle is: $area")
}

Exercise 2: Check if a Number is Even or Odd

Problem: Write a Kotlin program to check if a given number is even or odd.

Example Solution:

fun checkEvenOrOdd(number: Int): String {
return if (number % 2 == 0) "Even" else "Odd"
}

fun main() {
val number = 7
val result = checkEvenOrOdd(number)
println("$number is $result")
}

Exercise 3: Find the Maximum of Three Numbers

Problem: Write a Kotlin program to find the maximum of three given numbers.

Example Solution:

fun findMaximum(a: Int, b: Int, c: Int): Int {
return maxOf(a, b, c)
}

fun main() {
val num1 = 10
val num2 = 20
val num3 = 15
val max = findMaximum(num1, num2, num3)
println("The maximum of $num1, $num2, and $num3 is: $max")
}

Exercise 4: Check if a Year is Leap Year

Problem: Write a Kotlin program to check if a given year is a leap year.

Example Solution:

fun isLeapYear(year: Int): Boolean {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}

fun main() {
val year = 2024
val leap = if (isLeapYear(year)) "is" else "is not"
println("$year $leap a leap year")
}

These exercises cover basic concepts like functions, conditional statements, and mathematical operations in Kotlin. They are a good starting point for practicing Kotlin programming.

Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java