Basic arithmetic operations and comments in Kotlin

| Kotlin

In the following tutorial, we’re going to understand how to perform basic maths operations and writing comments in between program in Kotlin.

Note :-

  • This tutorial assumes, you have a basic working knowledge of IntelliJ Idea.
  • Have Kotlin installed setup in your machine.
  • Follow the last tutorials for setting up a new class file before you can begin writing script.

Before we begin, we need to have a main function. This main function is called upon when the file is being executed, just like Java’s main function.

Declaring comments :

There are two ways to put comments in Kotlin.

  1. Using // for in-line comments.
  2. Using /**/ for comments spanning multiple lines.

In Kotlin, similar to Java, just use // to declare an in-line comment or /**/ to declare a multi-line comment.

  fun main(args: Array<String>) {
    val date = "21st March" // This is an in-line comment
    /*
    This however
    is a multi-line
    comment
    */
  }

Basic arithmetic operations

Types of numbers in Kotlin

Kotlin has 5 Arithmetic Operators, similar to Java, listed below :

  • Addition operator (+)
  • Subtraction operator (-)
  • Multiplication operator (*)
  • Division operator (/)
  • Modulus operator (%)

Syntax

Addition Operator

var variable_one = variable_two + variable_three

For example:

var sum = 10+21

Subtraction Operator

var variable_one = variable_two - variable_three

For example:

var sub = 110-20

Multiplication Operator

var variable_one = variable_two * variable_three

For example:

var mul = 100*21

Division Operator

var variable_one = variable_two / variable_three

For example:

var div = 100/25

Modulus Operator

var variable_one = variable_two % variable_three

For example:

var mod = 25%6

Arithmetic Operations Example Program in Kotlin

fun main(args: Array<String>) {
    val num1 = 200;
    val num2 = 10

    //Addition
    val addition = num1+num2
    //Subtraction
    val subtraction = num1-num2
    //Multiplication
    val multiplication = num1*num2
    //Division
    val division = num1/num2
    //Modulus
    val modulus = num1%num2

    println("Sum of $num1 and $num2 is : $addition")
    println("Difference of $num1 and $num2 is : $subtraction")
    println("Product of $num1 and $num2 is : $multiplication")
    println("Division of $num1 and $num2 is : $division")
    println("Modulus of $num1 and $num2 is : $modulus")
}

Sample Output

Sum of 200 and 10 is : 210
Difference of 200 and 10 is : 190
Product of 200 and 10 is : 2000
Division of 200 and 10 is : 20
Modulus of 200 and 10 is : 0



Keep coding! Have fun.

Share this:

Website Developed and Maintained by - utkarsh-raghav-qa