top of page

Kotlin programs - Kotlin Assignment Help

Updated: Feb 15, 2023

In this blog, we are going learn some basics programs in kotlin



1. Kotlin program to perform arithmetic operations on two numbers:


fun main(args:Array<String>){
    val a = 13
    val b = 5
    val sum  = a + b 
    val sub  = a - b 
    val muti = a * b  
    val div  = a / b  
    val rem  = a % b 
    println("Addison of $a and $b is       : $sum")
    println("Subtraction of $a and $b is   : $sub")
    println("Multiplication of $a and $b is: $muti")
    println("Division of $a and $b is      : $div")
    println("Remainder of $a and $b is     : $rem")
}

Output

Run 1:
Enter Integer Value :
123
Integer Number is : 123
Enter Long Value :
34355566
Long Number is : 34355566
Enter Float Value :
45.34
Float Number is : 45.34
Enter Double Value :
456.78
Double Number is : 456.78



2. Program to perform simple calculations on two integer numbers in Kotlin

import java.util.*


fun main(args: Array<String>) {
   val reader = Scanner(System.`in`)

   
    print("Enter Integer Value : ")
    var first = reader.nextInt()
    print("Enter Integer Value : ")
    var second = reader.nextInt()

   
    print("Enter any Action( +, -, *, /, % ) :  ")
    val choice = reader.next()[0]

    try{
        var result = when(choice){
            '+' -> first+second
            '-' -> first-second
            '*' -> first*second
            '/' -> first/second
            '%' -> first%second
            else -> {
                System.err.println("Not a Valid Operation choice")
                return
            }
        }
        println("Result is : $result")
    }catch (E:Exception){
        System.err.println("Exception  : ${E.toString()}")
    }
}

Output:

RUN 1:
Enter Integer Value : 35
Enter Integer Value : 8
Enter any Action( +, -, *, /, % ) :  %
Result is : 3
---
RUN 2:
Enter Integer Value : 456
Enter Integer Value : 67
Enter any Action( +, -, *, /, % ) :  /
Result is : 6



3. Program to input and print numbers in Kotlin

fun main(args: Array<String>) {
    var reader = Scanner(System.`in`)
    
    println("Enter Integer Value : ")
    val intValue = reader.nextInt()
    println("Integer Number is : $intValue")
    
    println("Enter Long Value : ")
    val longValue = reader.nextLong()
    println("Long Number is : $longValue")
    
    println("Enter Float Value : ")
    val floatValue = reader.nextFloat()
    println("Float Number is : $floatValue")
    
    println("Enter Double Value : ")
    val doubleValue = reader.nextDouble()
    println("Double Number is : $doubleValue")
}

Output

Run 1:
Enter Integer Value :
123
Integer Number is : 123
Enter Long Value :
34355566
Long Number is : 34355566
Enter Float Value :
45.34
Float Number is : 45.34
Enter Double Value :
456.78
Double Number is : 456.78



4. Program to input a string in Kotlin

Hfun main(args: Array<String>) {
    
    println("Enter Your Name: ")
    val name = readLine()
    
    println("Enter Your Address: ")
    val add = readLine()
    
    println("Enter Your Country Name : ")   
    val country = readLine()
    
    println("Name: $name")
    println("Address: $add")
    println("County: $country")
}

Output

Enter Your Name: 
Shivang
Enter Your Address: 
Indore, sector 73
Enter Your Country Name : 
India
Name: Shivang
Address: Indore, sector 73
County: India

Hope now you are able to understand the several programs or how to write code in kotlin in the next blog we are going to learn about Nullabilty in kotlin.

Thank you


The journey of solving bug and completing project on time in Kotlin can be challenging and lonely. If you need help regarding other sides to Kotlin, we’re here for you!

Drop a email to us at contact@codersarts.com with Project title, deadline and requirement files. Our email team will revert back promptly to get started the work.

Commentaires


bottom of page