Skip to content

Commit

Permalink
Reto mouredev#14 - Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
BanniaWP committed Apr 4, 2023
1 parent 3280fbd commit 124a1bd
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Retos/Reto #14 - OCTAL Y HEXADECIMAL [Fácil]/kotlin/pisanowp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
fun main() {

/*
* Reto #14 03/04/2023
* Crea una función que reciba un número decimal y lo trasforme a Octal
* y Hexadecimal.
* - No está permitido usar funciones propias del lenguaje de programación que
* realicen esas operaciones directamente.
*/


val listaNumeros = listOf(0, 3, 8, 12, 16, 57, 200, 800, 1332, 8149)

listaNumeros.forEach { numero ->
println("El numero $numero es ${numero.toOctal()} en OCTAL y ${numero.toHexadecimal()} en HEXADECIMAL")
}

}

fun Int.toOctal(): String {
var octal = ""
var numero = this
var resto: Int

if (this == 0)
octal = "0"

while (numero > 0) {
resto = numero % 8
octal = resto.toString() + octal
numero /= 8

}
return octal
}

fun Int.toHexadecimal(): String {
val letras = listOf('A', 'B', 'C', 'D', 'E', 'F')
var digitoChar : String
var hexadecimal = ""
var numero = this
var resto: Int

if (this == 0)
hexadecimal = "0"

while (numero > 0) {
resto = numero % 16
if (resto < 10) {
digitoChar = resto.toString()
}
else {
digitoChar = letras[resto-10].toString()
}

hexadecimal = digitoChar + hexadecimal
numero /= 16

}
return hexadecimal

}

0 comments on commit 124a1bd

Please sign in to comment.