Download kotlin Programming APP on PlayStore

Download Website SEO Lessons APP on PlayStore

- Calculate Power with kotlin Programming Language:
- Calculate Factorial with kotlin programming Language:
- Mathematical Approaches in kotlin programming Language
- Calculate Exp(x) with kotlin programming Language:
- Calculate sin(x) with kotlin Programming Language:
- Calculate Cos (x) with kotlin Programming Language:

In this tutorial, we will program trigonometric functions of a scientific calculator, with the syntax of Kotlin, the cool programming alternative in Java.

Although many languages have a library to perform these numerical calculations, today I want to show you what algorithms are behind these black boxes. We will perform this exercise in the Kotlin programming language, an alternative to Java, which executes all executables in the classic JVM.

Topic: Scientific Mathematics Calculator using Kotlin Programming Language.

Ok, let’s begin! In the Kotlin programming language, the functions are much easier to create. Let’s make the following comparison.

**Java:**

```
public static double sum(double A, double b) {
return a+b;
}
```

**Kotlin:**

```
fun sum( a: Double, b: Double): Double {
return a+b
}
```

The syntax of Kotlin is very similar to many friendly programming languages like Python and javascript, while still being a static typing language.

Topic: Scientific Mathematics Calculator using Kotlin Programming Language.

Even in this function has a statement the statement can be reduced, regardless of the keyword return and the keys.

`fun sum( a: Double, b: Double): Double = a+b`

This way we can create our basic arithmetic functions in a faster way.

```
fun sum( a: Double, b: Double): Double = a + b
fun rest( a: Double, b: Double): Double = a - b
fun mult( a: Double, b: Double): Double = a * b
fun div( a: Double, b: Double): Double = a / b
```

## Calculate Power with kotlin Programming Language:

The logic of this operation tells us that we must multiply the number of times of the second parameter to the first parameter.

```
fun pot( a: Double, b: Int): Double {
var result:Double = 1.0
for(i in 1..b)
result = result*a
return result
}
```

In the result variable, the successive multiplications performed by the first parameter to itself are stored. Here we see that the syntax of Kotlin allows us to use, whether or not we want the brackets to delimit a block, the syntax of the for loop is also much more readable.

Now that you can test the result of the created function, you must create a main function.

```
fun main(args: array) {
println(pot(2.0,5))
}
```

## Calculate Factorial with kotlin programming Language:

This operation is a result of multiplying a number by all positive integers smaller than the.

```
fun fact( a: Int): Double {
var result:Double = 1.0
for(i in a downTo 1)
result = result*i
return result
}
```

Topic: Scientific Mathematics Calculator using Kotlin Programming Language.

The factorial value is accumulated in the result variable.

In the loop used in this function, to downlink from a to 1 the keyword downTo is used.

This function can also be tested in the main function.

`println(pot(2.0,5))`

## Mathematical Approaches in kotlin programming Language

The way computers calculate trigonometric functions are approximations made using the Taylor series.

The McLaurin series is the particular case where a = 0.

Based on this series are generated the geometric series that give rise to the calculations of trigonometric functions. In this tutorial we will program 3 functions exp (x), sin (x) and cos (x).

## Calculate Exp(x) with kotlin programming Language:

The series defining this function is as follows:

Let’s program this series up to a certain number, for example, 40.

```
fun exp( x: Double): Double {
var result:Double = 0.0
for(n in 0..40)
result = result+(pot(x,n)/fact(n))
return result
}
```

The higher the number of iterations the answer will be more precise, but there comes a point where one more iteration does not alter the result and any extra iteration will be a computational waste. To solve this we will create a conditional that will compare the current result with the previous one, if they are the same then the iterative process will end.

Topic: Scientific Mathematics Calculator using Kotlin Programming Language.

```
fun exp( x: Double): Double{
var result:Double = 0.0
for(n in 0..40){
val anterior = result
result = result+(pot(x,n)/fact(n))
if (anterior == result) break
}
return result
}
```

This function can also be tested in the main function.

`println(exp(1))`

## Calculate sin(x) with kotlin Programming Language:

The series defining this function is as follows:

Let’s program this series up just like in the previous one with a conditional.

```
fun sin( x: Double): Double{
var result:Double = 0.0
for(n in 0..40){
val anterior = result
result = result+(pot(-1.0,n)/fact(2*n+1)*pot(x,2*n+1))
if (anterior == result) break
}
return result
}
```

This function can also be tested in the main function.

`println(sin(3.14159265/2)) // sin(Pi/2) = 1`

## Calculate Cos (x) with kotlin Programming Language:

The series defining this function is as follows:

Let’s program this series up just like in the previous one with a conditional.

```
fun sin( x: Double): Double{
var result:Double = 0.0
for(n in 0..40){
val anterior = result
result = result+(pot(-1.0,n)/fact(2*n)*pot(x,2*n))
if (anterior == result) break
}
return result
}
```

This function can also be tested in the main function.

`println(sin(3.14159265)) // sin(Pi) = 1`