Last active
August 31, 2018 20:16
-
-
Save williaanlopes/98845b9b68a7e4197fe329035ab28a98 to your computer and use it in GitHub Desktop.
Invert a number using recursion in various languages. (Inverter um numero usando recursividade em várias linguagens.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Linguagem C | |
// funcao recursiva para expoente | |
int expoente(int e) | |
{ | |
if(e == 0) { | |
return 0; | |
} else { | |
return 1 + expoente (e/10); | |
} | |
} | |
// funcao recursisa que inverte o numero | |
int inverte(int n) | |
{ | |
if(n == 0) { | |
return 0; | |
} else { | |
return ((n%10) * pow(10, expoente (n/10))) + inverte (n/10); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Linguagem HASKELL | |
-- funcao recursiva para expoente | |
expoente :: Int -> Int | |
expoente e | |
| e == 0 = 0 | |
| otherwise = 1 + expoente (div e 10) | |
-- funcao recursisa que inverte o numero | |
inverte :: Int -> Int | |
inverte n | |
| n == 0 = 0 | |
| otherwise = ((mod n 10) * (10 ^ expoente (div n 10))) + inverte (div n 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment