Kotlin example

Kotlin example for Beginners – Kotlin Program to Concatenate Two Arrays

Kotlin Program to Concatenate Two Arrays In this program, you’ll learn to concatenate two arrays in Kotlin using arraycopy and without it. Example 1: Concatenate Two Arrays using arraycopy import java.util.Arrays fun main(args: Array<String>) { val array1 = intArrayOf(1, 2, 3) val array2 = intArrayOf(4, 5, 6) val aLen = array1.size val bLen = array2.size …

Kotlin example for Beginners – Kotlin Program to Print an Array

Kotlin Program to Convert String to Date In this program, you’ll learn to convert string to date in Kotlin using formatter. Example 1: Convert String to Date using predefined formatters import java.time.LocalDate import java.time.format.DateTimeFormatter fun main(args: Array<String>) { // Format y-M-d or yyyy-MM-d val string = “2017-07-25” val date = LocalDate.parse(string, DateTimeFormatter.ISO_DATE) println(date) } When …

Kotlin example for Beginners – Kotlin Code To Create Pyramid and Pattern

Kotlin Code To Create Pyramid and Pattern In this program, you’ll learn to create pyramid, half pyramid, inverted pyramid, Pascal’s triangle and Floyd’s triangle sing control statements in Kotlin.   Programs to print triangles using *, numbers and characters Example 1: Program to print half pyramid using * * * * * * * * …

Kotlin example for Beginners – Kotlin Program to Add Two Complex Numbers

Kotlin Program to Add Two Complex Numbers by Passing Class to a Function In this program, you’ll learn to add two complex numbers in Kotlin by creating a class named Complex and passing it into a function add(). Example: Add Two Complex Numbers class Complex(internal var real: Double, internal var imag: Double) fun main(args: Array<String>) …

Kotlin example for Beginners – Kotlin Program to Count the Number of Vowels and Consonants in a Sentence

Kotlin Program to Count the Number of Vowels and Consonants in a Sentence In this program, you’ll learn to count the number of vowels, consonants, digits and spaces in a given sentence in Kotlin. Example 1: Program to count vowels, consonants, digits and spaces fun main(args: Array<String>) { var line = “This website is aw3som3.” …

Kotlin example for Beginners – Kotlin Program to Multiply to Matrix Using Multi-dimensional Arrays

Kotlin Program to Multiply to Matrix Using Multi-dimensional Arrays In this program, you’ll learn to multiply two matrices using multi-dimensional arrays in Kotlin. For matrix multiplication to take place, the number of columns of first matrix must be equal to the number of rows of second matrix. In our example, i.e. c1 = r2 Also, …

Kotlin example for Beginners – Kotlin Program to Calculate Standard Deviation

Kotlin Program to Calculate Standard Deviation In this program, you’ll learn to calculate the standard deviation using a function in Kotlin. This program calculates the standard deviation of a individual series using arrays. Visit this page to learn about Standard Deviation. To calculate the standard deviation, calculateSD() function is created. The array containing 10 elements is passed to …

Kotlin example for Beginners – Kotlin Program to Calculate Average Using Arrays

Kotlin Program to Calculate Average Using Arrays In this program, you’ll learn to calculate the average of the given arrays in Kotlin. Example: Program to Calculate Average Using Arrays fun main(args: Array<String>) { val numArray = doubleArrayOf(45.3, 67.5, -45.6, 20.34, 33.0, 45.6) var sum = 0.0 for (num in numArray) { sum += num } …

Kotlin example for Beginners – Kotlin Program to calculate the power using recursion

Kotlin Program to calculate the power using recursion In this program, you’ll learn to calculate the power of a number using a recursive function in Kotlin. Example: Program to calculate power using recursion fun main(args: Array<String>) { val base = 3 val powerRaised = 4 val result = power(base, powerRaised) println(“$base^$powerRaised = $result”) } fun …