From 11260530f76a5147a8d4d60fa3b55b7e002293e7 Mon Sep 17 00:00:00 2001 From: ellav Date: Sun, 13 Oct 2019 22:15:28 +0200 Subject: [PATCH] reverses digits of integer --- DigitReverser.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 DigitReverser.cpp diff --git a/DigitReverser.cpp b/DigitReverser.cpp new file mode 100644 index 0000000..d6e8c33 --- /dev/null +++ b/DigitReverser.cpp @@ -0,0 +1,47 @@ +/* +Write a function, reverseDigit, that takes an integer as a parameter and returns the number with its digits reversed. For example, the value of reverseDigit(12345) is 54321; the value of reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532) is -235. + +You may name the program as you please, for example, "DigitReverser.cpp" + +Please make sure the program compiles and runs as it should! +*/ + +#include + +using namespace std; + + +int swap_digit(int x) +{ + static int revers = 0; + static int base = 1; + + if (x > 0){ + swap_digit(x/10); + revers += (x%10)*base; + base *= 10; + } + return revers; +} + +int swap_negativ_digits(int x) +{ + int res; + if (x < 0){ + x = x*(-1); + res = swap_digit(x); + res = res*(-1); + } else if (x == 0){ + res = 0; + } else if (x > 0){ + res = swap_digit(x); + } + return res; +} + + +int main () +{ + int res = swap_negativ_digits(-12000); + cout << res; +} \ No newline at end of file