30 Kotlin Basic Exercises for Beginners with Solutions

Master beginner Kotlin skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in Kotlin, setting a solid foundation for intermediate challenges. Start your journey to Kotlin mastery today!

Learning Objectives:

Learn Kotlin syntax, basic data types, control flow, and functions to write simple, readable programs. Develop foundational programming logic and understand Kotlin’s concise and expressive structure.

Exercise Instructions:

  • Start with the first exercise and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you in more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Write a Kotlin program to print "Hello, World!".

Required Input:

No input required.

Expected Output:

Hello, World!

Code In Kotlin

fun main() { // Add your code here }

Run Code?

Click Run Button to view compiled output

2. Write a program to add two numbers and print the result.

Required Input:

Two hardcoded integers, e.g., 5 and 10.

Expected Output:

The sum is: 15

Code In Kotlin

fun main() { val a = 5 val b = 10 // Add your code here to calculate the sum }

Run Code?

Click Run Button to view compiled output

3. Create a program to check if a number is even or odd.

Required Input:

A hardcoded integer, e.g., 7.

Expected Output:

7 is odd

Code In Kotlin

fun main() { val number = 7 // Add your code here to check if the number is even or odd }

Run Code?

Click Run Button to view compiled output

4. Write a Kotlin program to find the largest of three numbers.

Required Input:

Three hardcoded integers, e.g., 3, 7, and 5.

Expected Output:

The largest number is: 7

Code In Kotlin

fun main() { val a = 3 val b = 7 val c = 5 // Add your code here to find the largest number }

Run Code?

Click Run Button to view compiled output

5. Create a program to print the multiplication table of a number.

Required Input:

A hardcoded integer, e.g., 5.

Expected Output:

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Code In Kotlin

fun main() { val number = 5 // Add your code here to print the multiplication table }

Run Code?

Click Run Button to view compiled output

6. Write a Kotlin function to calculate the factorial of a number.

Required Input:

A hardcoded integer, e.g., 5.

Expected Output:

The factorial of 5 is: 120

Code In Kotlin

fun factorial(n: Int): Int { // Add your code here to calculate the factorial } fun main() { val number = 5 println("The factorial of $number is: ${factorial(number)}") }

Run Code?

Click Run Button to view compiled output

7. Write a program to reverse a string.

Required Input:

A hardcoded string, e.g., "hello".

Expected Output:

Reversed string: olleh

Code In Kotlin

fun main() { val input = "hello" // Add your code here to reverse the string }

Run Code?

Click Run Button to view compiled output

8. Write a program to check if a string is a palindrome.

Required Input:

A hardcoded string, e.g., "madam".

Expected Output:

madam is a palindrome

Code In Kotlin

fun main() { val input = "madam" // Add your code here to check if the string is a palindrome }

Run Code?

Click Run Button to view compiled output

9. Create a Kotlin program to calculate the sum of an array.

Required Input:

A hardcoded array, e.g., [1, 2, 3, 4, 5].

Expected Output:

The sum of the array is: 15

Code In Kotlin

fun main() { val numbers = arrayOf(1, 2, 3, 4, 5) // Add your code here to calculate the sum }

Run Code?

Click Run Button to view compiled output

10. Write a program to find the largest element in an array.

Required Input:

A hardcoded array, e.g., [10, 20, 5, 30].

Expected Output:

The largest element is: 30

Code In Kotlin

fun main() { val numbers = arrayOf(10, 20, 5, 30) // Add your code here to find the largest element }

Run Code?

Click Run Button to view compiled output

ad vertical

1 of 3