Posts

Showing posts from August, 2026

Arrays in Kotlin | Kotlin Functional Programming

Image
  package com.example.my_kotlin_practice fun main (args: Array<String>) { val array1 = arrayOf <Int> ( 10 , 1 , 20 , 30 , 40 , 50 ) println (array1. joinToString ()) val array2 = arrayOf <String> ( "CS" , "IT" , "Chemistry" , "Physics" ) // println(array2.contentToString()) for (i in array2) { println (i) } for ((i,e) in array2. withIndex ()) { println ( " $ i : $ e " ) } array2[ 2 ] = "Maths" array2.set( 3 , "Biology" ) println ( "array2[2] : ${ array2[ 2 ] } " ) println (array2.get( 3 )) } OUTPUT : 10, 1, 20, 30, 40, 50 CS IT Chemistry Physics 0 : CS 1 : IT 2 : Chemistry 3 : Physics array2[2] : Maths Biology Process finished with exit code 0

Kotlin Functions | Kotlin Functional Programming

Image
  package com.example.my_kotlin_practice fun main (args: Array<String>){ var a:Int = 10 var b:Int = 30 var add = addition (a) println ( "Addition is : $ add " ) var add1 = addition1 ( a1 = 20 , a2 = 80 ) //These are called "named arguments" println ( "Addition1 is : $ add1 " ) var add3 = addition ( 4 , 5 , 8 ) println ( "Addition3 is : $ add3 " ) addition2 ( a1= 20 , a2= 100 ) var add2 = ::addition3 println (add2( 2 , 5 )) } fun addition (a1:Int,a2: Int = 40 ): Int{ return a1+a2 } fun addition (a1:Int,a2:Int,a3: Int):Int { return a1+a2+a3 } //Below is another way(single line function) to define function if there is only single expression //fun addition(a1:Int,a2: Int = 40) = a1+a2 fun addition1 (a1:Int,a2: Int):Int { return a1+a2 } fun addition2 (a1:Int,a2: Int) { println (a1+a2) } fun addition3 (a1: Int,a2: Int): Int { return a1+a2 } OUTPUT : Addition is : 50 Addition1 is : ...

Kotlin For Loop

  package com.example.my_kotlin_practice fun main (args: Array<String>) { for (i in 1 .. 10 ) { println ( "The value of 'i' is : $ i " ) } print ( " \n " ) for (i in 1 .. 10 step 2 ) { println (i) } print ( " \n " ) for (i in 1 until 10 ) { println ( "The value of 'i'(using 'until') is : $ i " ) } print ( " \n " ) for (i in 10 downTo 1 ) { println ( "The value of 'i'(using 'downTo') is : $ i " ) } print ( " \n " ) val num = 3 for (i in 1 .. 10 ) { println ( " $ num * $ i = ${ num*i } " ) } } OUTPUT : The value of 'i' is : 1 The value of 'i' is : 2 The value of 'i' is : 3 The value of 'i' is : 4 The value of 'i' is : 5 The value of 'i' is : 6 The value of 'i' is : 7 The value of 'i' is : 8 The value ...

Kotlin While & Do-While Loops

  package com.example.my_kotlin_practice fun main (args: Array<String>) { var i:Int = 1 while (i<= 5 ) { println ( "The value of 'i'(using while loop) is : $ i " ) i++ } print ( " \n " ) var i1:Int = 5 while (i1> 0 ) { println ( "The value of 'i1'(using while loop) is : $ i1 " ) i1-- } print ( " \n " ) var i2:Int = 10 do { println ( "The value of 'i2'(using do..while loop) is : $ i2 " ) i2++ } while (i2<= 5 ) print ( " \n " ) var i3:Int = 5 do { println ( "The value of 'i3'(using do..while loop) is : $ i3 " ) i3-- } while (i3> 0 ) } OUTPUT : The value of 'i'(using while loop) is : 1 The value of 'i'(using while loop) is : 2 The value of 'i'(using while loop) is : 3 The value of 'i'(using while loop) is : 4 The valu...

Kotlin When Expression & When Statements

  package com.example.my_kotlin_practice fun main (args: Array<String>) { val a:Int = 50 val b:Int = 10 val opr:String = "add" when (opr) { "add" -> { var c = a + b println ( "Addition is : $ c " ) } "sub" -> println ( "Subtraction is : ${ a-b } " ) "mul" -> println ( "Multiplication is : ${ a*b } " ) "div" -> println ( "Division is : ${ a/b } " ) "mod" -> println ( "Modulus is : ${ a%b } " ) else -> println ( "Wrong operation specified" ) } val c = when (opr) { "add" -> a+b "sub" -> a-b "mul" -> a*b "div" -> a/b "mod" -> a%b else -> 0 } println ( "Result is : $ c " ) var a1:Int = 13 when (a1) { ...

Kotlin Conditions | If-Else Statements in Kotlin

package com.example.my_kotlin_practice fun main (args: Array<String>){ val a:Int = 35 val b:Int = 40 if (a>b) { println ( " $ a is greater than $ b " ) } else { println ( " $ b is greater than $ a " ) } println ( if (a>b) a else b) val marks:Int = 100 val grade = if (marks> 60 ) "Excellent" else if (marks< 60 && marks> 45 ) "Good" else "Poor" println ( "Your grade is : $ grade " ) } OUTPUT : 40 is greater than 35 40 Your grade is : Excellent Process finished with exit code 0 

Kotlin Operator | Logical, Increment & Decrement Operator

Image
  package com.example.my_kotlin_practice fun main (args: Array<String>) { val l:Int = 10 val m:Int = 20 val n:Int = 30 println ( "Logical operators : " ) println ( "l>m && n>m : ${ l>m && n>m } " ) println ( "l<m && n>m : ${ l<m && n>m } " ) println ( "l>m || n>m : ${ l>m || n>m } " ) println ( "l>m || m>n : ${ l>m || m>n } " ) println ( "!(l>m) : ${ !(l>m) } " ) println ( "!(l<m) : ${ !(l<m) } " ) println ( "Increment and decrement operators : " ) var v:Int = 10 println ( "++v : ${ ++v } " ) println ( "v++ : ${ v++ } " ) println ( "v : $ v " ) println ( "--v : ${ --v } " ) println ( "v-- : ${ v-- } " ) println ( "v : $ v " ) } OUTPUT : Logical operators : l>m && n>m...

Kotlin Operator | Arithmetic, Assignment & Relational Operator

Image
  package com.example.my_kotlin_practice fun main (args: Array<String>) { val a:Int = 15 val b:Int = 8 println ( "Arithmetic operators : " ) println ( "Addition(a+b)= ${ a+b } " ) println ( "Subtraction(a-b)= ${ a-b } " ) println ( "Multiplication(a*b)= ${ a*b } " ) println ( "Division(a/b)= ${ a/b.toFloat() } " ) println ( "Modulus(a%b)= ${ a%b } " ) println ( "Assignment operators : " ) var x:Int = 10 x+= 10 println ( "x+=10 : $ x " ) x-= 10 println ( "x-=10 : $ x " ) x*= 10 println ( "x*=10 : $ x " ) x/= 10 println ( "x/=10 : $ x " ) x%= 10 println ( "x%=10 : $ x " ) println ( "Relational operators : " ) val i:Int = 10 val j:Int = 20 println ( "i>j : ${ i>j } " ) println ( "i<j : ${ i<j } " ) println ( "i>=j...