Basic Calculator

fun main() {
    println("Enter Two Integer Number")//1
    val first = readLine()?.toInt()!!//2
    val second = readLine()?.toInt()!!//3
    println("Enter your choice\n A for Addition\n M for Multiply \n D for Division \n S for Substation")//4
    val userChoice= readLine()//5
    val result = when(userChoice)//6
    {
        "A","a"-> "Addition of Number $first+$second=${first+second}"//7
        "S","s"-> "SubStraction of Two Number $first-$second=${first-second}"//8
        "M","m"->  "Multiplication of Two Number $first*$second =${first*second}"//9
        "D","d"->  "Division of Two Number= $first/$second=${first/second}"//10
        else->"Sorry Wrong Choice"//11
    }
    println("Calculation Result=$result")//12
}
  1. Instruct the user to Enter two number for calculation
  2. The first Number entered by user convert into int and assigned to the First variable
  3. The Second Number entered by user convert into int and assigned to the second variable
  4. Instruct the user to Enter their choice to perform the calculation
  5. The choice entered by the user assigned to the choice variable
  6. When statement result based on user choice assigned to result variable
  7. Operation addition is performed if the user enters “A” or “a”
  8. Operation subtraction is performed if the user enters “S” or “s”
  9. Operation multiplication is performed if the user enters “M” or “m”
  10. Operation Division is performed if the user enters “D” or “d”
  11. if Enter wrong input mentioned in 7,8,9,10 then else part execute
  12. Finally, Result is print on command prompt

Output of Program

Addition:-

Enter Two Integer Number

20

30

Enter your choice

A for Addition

M for Multiply

D for Division

S for Substation

a

Calculation Result=Addition of Number 20+30=50

Multiplication

Enter Two Integer Number

30

50

Enter your choice

A for Addition

M for Multiply

D for Division

S for Substation

m

Calculation Result=Multiplication of Two Number 30*50 =1500

Substation:-

Enter Two Integer Number

45

50

Enter your choice

A for Addition

M for Multiply

D for Division

S for Substation

s

Calculation Result=SubStraction of Two Number 45-50=-5

Division:-

Enter Two Integer Number

200

40

Enter your choice

A for Addition

M for Multiply

D for Division

S for Substation

d

Calculation Result=Division of Two Number= 200/40=5

Leave a Reply

Your email address will not be published. Required fields are marked *