11. Write a function to reverse the order of words in a string.
Required Input:
A hardcoded string, e.g., "Kotlin is awesome"
Expected Output:
Reversed words: awesome is Kotlin
Code In Kotlin
fun reverseWords(input: String): String {
// Add your code here to reverse the words
}
fun main() {
val input = "Kotlin is awesome"
println("Reversed words: ${reverseWords(input)}")
}
Run Code?
Click Run Button to view compiled output
12. Create a function to find the first missing positive number in an array.
Required Input:
A hardcoded array, e.g., [3, 4, -1, 1]
Expected Output:
First missing positive: 2
Code In Kotlin
fun firstMissingPositive(nums: Array<Int>): Int {
// Add your code here to find the first missing positive
}
fun main() {
val nums = arrayOf(3, 4, -1, 1)
println("First missing positive: ${firstMissingPositive(nums)}")
}
Run Code?
Click Run Button to view compiled output
13. Write a function to calculate the power of a number iteratively.
Required Input:
Hardcoded values for base 2 and exponent 5.
Expected Output:
2 raised to the power of 5 is: 32
Code In Kotlin
fun calculatePower(base: Int, exponent: Int): Int {
// Add your code here to calculate the power
}
fun main() {
val base = 2
val exponent = 5
println("$base raised to the power of $exponent is: ${calculatePower(base, exponent)}")
}
Run Code?
Click Run Button to view compiled output
14. Implement a function to calculate the factorial of a number using a single loop.
Required Input:
A hardcoded number, e.g., 5.
Expected Output:
Factorial of 5 is: 120
Code In Kotlin
fun factorial(n: Int): Int {
// Add your code here to calculate the factorial
}
fun main() {
val n = 5
println("Factorial of $n is: ${factorial(n)}")
}
Run Code?
Click Run Button to view compiled output
15. Write a function to find all numbers in an array that are divisible by a given number.
Required Input:
Hardcoded array [10, 15, 20, 25, 30] and divisor 5.
Expected Output:
Numbers divisible by 5: [10, 15, 20, 25, 30]
Code In Kotlin
fun findDivisibleNumbers(nums: Array<Int>, divisor: Int): List<Int> {
// Add your code here to find divisible numbers
}
fun main() {
val nums = arrayOf(10, 15, 20, 25, 30)
val divisor = 5
println("Numbers divisible by $divisor: ${findDivisibleNumbers(nums, divisor)}")
}
Run Code?
Click Run Button to view compiled output
16. Create a program to find the longest common prefix among an array of strings.
Required Input:
A hardcoded array, e.g., ["flower", "flow", "flight"].
Expected Output:
Longest common prefix: fl
Code In Kotlin
fun longestCommonPrefix(strings: Array<String>): String {
// Add your code here to find the longest common prefix
}
fun main() {
val strings = arrayOf("flower", "flow", "flight")
println("Longest common prefix: ${longestCommonPrefix(strings)}")
}
Run Code?
Click Run Button to view compiled output
17. Write a function to convert a decimal number to its binary representation.
Required Input:
A hardcoded number, e.g., 10.
Expected Output:
Binary representation of 10 is: 1010
Code In Kotlin
fun decimalToBinary(number: Int): String {
// Add your code here to convert to binary
}
fun main() {
val number = 10
println("Binary representation of $number is: ${decimalToBinary(number)}")
}
Run Code?
Click Run Button to view compiled output
18. Implement a function to count the frequency of each character in a string.
Required Input:
A hardcoded string, e.g., "hello".
Expected Output:
Character frequencies: {h=1, e=1, l=2, o=1}
Code In Kotlin
fun countCharacterFrequency(input: String): Map<Char, Int> {
// Add your code here to count character frequency
}
fun main() {
val input = "hello"
println("Character frequencies: ${countCharacterFrequency(input)}")
}
Run Code?
Click Run Button to view compiled output
19. Write a function to rotate an array by k elements to the left.
Required Input:
A hardcoded array [1, 2, 3, 4, 5] and k = 2.
Expected Output:
Rotated array: [3, 4, 5, 1, 2]
Code In Kotlin
fun rotateArrayLeft(nums: Array<Int>, k: Int): List<Int> {
// Add your code here to rotate the array
}
fun main() {
val nums = arrayOf(1, 2, 3, 4, 5)
val k = 2
println("Rotated array: ${rotateArrayLeft(nums, k)}")
}
Run Code?
Click Run Button to view compiled output
20. Create a function to check if a number is an Armstrong number.
Required Input:
A hardcoded number, e.g., 153.
Expected Output:
153 is an Armstrong number
Code In Kotlin
fun isArmstrongNumber(num: Int): Boolean {
// Add your code here to check if the number is Armstrong
}
fun main() {
val num = 153
if (isArmstrongNumber(num)) {
println("$num is an Armstrong number")
} else {
println("$num is not an Armstrong number")
}
}
Run Code?
Click Run Button to view compiled output