Skip to content

Commit

Permalink
Merge pull request #1578 from itsIapetus/WOC-Contributions
Browse files Browse the repository at this point in the history
Kotlin Algorithms
  • Loading branch information
shraddhavp authored Dec 18, 2020
2 parents d5ce012 + 700b8aa commit e0462f6
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
51 changes: 51 additions & 0 deletions Kotlin/Maths/Areas2D.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package kotlin.math

// Area of a Square

fun square(a: Double): Double {
return a * a
}

// Area of a Reactangle

fun rectangle(l: Double, b: Double): Double{
return l * b
}

// Area of a Circle

fun circle(r: Double): Double{
return PI * r * r
}

// Area of a Triangle

fun triangle(b: Double, h: Double): Double{
return b * h/2.0
}

// Area of a Parallelogram

fun parallelogram(b: Double, h: Double): Double{
return b * h
}

// Area of a Trapezium

fun trapezium(a: Double, b:Double, h:Double): Double{
return (a+b)*h/2.0
}


fun main() {
println(square(2.0))
println(rectangle(2.0, 4.6))
println(circle(5.0))
println(triangle(2.0,3.0))
println(parallelogram(4.5,2.3))
println(trapezium(1.5,4.5,6.0))
}


// Time Complexity - O(n)
// Space Complexity - O(n)
50 changes: 50 additions & 0 deletions Kotlin/Maths/Areas3D.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package kotlin.math

// Area of a Cube

fun cube(a: Double): Double {
return 6 * a * a
}

// Area of a Cuboid

fun cuboid(l: Double, b: Double, h: Double): Double{
return 2 * ((l*b) + (b*h) + (h*l))
}

// Area of a Cone

fun cone(r: Double, l: Double): Double{
return (PI * r * r) + (PI * r * l)
}

// Area of a Cylinder

fun cylinder(r: Double, h: Double): Double{
return (2.0 * (PI * r * r)) + (r * h)
}

// Area of a Sphere

fun sphere(r: Double): Double{
return 4.0 * PI * r * r
}
// Area of a Hemi Sphere

fun hemisphere(r: Double): Double{
return (2.0 * PI * r * r) + (PI * r * r)
}


fun main() {
println(cube(2.0))
println(cuboid(2.0, 4.6, 3.5))
println(cone(5.0, 4.0))
println(cylinder(2.0,4.0))
println(sphere(4.5))
println(hemisphere(2.5))
}


// Time Complexity - O(n)
// Space Complexity - O(n)
7 changes: 6 additions & 1 deletion Kotlin/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@

## Sorting Algorithms

[Bubble Sort](sort/BubbleSort/src/BubbleSort.kt)
- [Bubble Sort](sort/BubbleSort/src/BubbleSort.kt)

## Maths

- [Areas 2D](Maths/Areas2D.kt)
- [Areas 3D](Maths/Areas3D.kt)

0 comments on commit e0462f6

Please sign in to comment.