From e42f4bb0b1e7f7b7413e95038eb8f9aac997e45e Mon Sep 17 00:00:00 2001 From: Ashad <93534298+Ashad001@users.noreply.github.com> Date: Thu, 11 Aug 2022 23:52:36 +0500 Subject: [PATCH] chore(CPlusPlus): add reverse number algorithm (#789) Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com> --- algorithms/C/maths/fibonacci-number/src/fib.c | 2 +- algorithms/C/maths/fibonacci-number/src/fib.h | 7 ++-- .../C/maths/fibonacci-number/src/main.c | 2 +- algorithms/CPlusPlus/README.md | 1 + .../CPlusPlus/Recursion/reverse-a-number.cpp | 34 +++++++++++++++++++ 5 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 algorithms/CPlusPlus/Recursion/reverse-a-number.cpp diff --git a/algorithms/C/maths/fibonacci-number/src/fib.c b/algorithms/C/maths/fibonacci-number/src/fib.c index 41196239..25ead35d 100644 --- a/algorithms/C/maths/fibonacci-number/src/fib.c +++ b/algorithms/C/maths/fibonacci-number/src/fib.c @@ -1,4 +1,4 @@ -#include"fib.h" +#include"./fib.h" // NOLINT(build/include) #include // sqrt, pow are used in binet_fib int recur_fib(int n){ diff --git a/algorithms/C/maths/fibonacci-number/src/fib.h b/algorithms/C/maths/fibonacci-number/src/fib.h index c770d96d..febadaf1 100644 --- a/algorithms/C/maths/fibonacci-number/src/fib.h +++ b/algorithms/C/maths/fibonacci-number/src/fib.h @@ -1,5 +1,6 @@ -#ifndef FIB_H_INCLUDED -#define FIB_H_INCLUDED + +#ifndef ALGORITHMS_C_MATHS_FIBONACCI_NUMBER_SRC_FIB_H_ +#define ALGORITHMS_C_MATHS_FIBONACCI_NUMBER_SRC_FIB_H_ /** * fib(n) takes nonnegative number n @@ -19,4 +20,4 @@ int iter_log_fib(int n); int log_fib(int n); int binet_fib(int n); -#endif \ No newline at end of file +#endif // ALGORITHMS_C_MATHS_FIBONACCI_NUMBER_SRC_FIB_H_" diff --git a/algorithms/C/maths/fibonacci-number/src/main.c b/algorithms/C/maths/fibonacci-number/src/main.c index 8bc173fc..7e0759db 100644 --- a/algorithms/C/maths/fibonacci-number/src/main.c +++ b/algorithms/C/maths/fibonacci-number/src/main.c @@ -1,5 +1,5 @@ #include -#include"fib.h" +#include"./fib.h" // NOLINT(build/include) int main(){ memomizing_fib(); // this is to initialize the memomized table diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index c545fa88..0350073e 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -186,6 +186,7 @@ - [Product of two numbers](Recursion\product-of-numbers.cpp) - [Product of digits in a number](Recursion\product-of-digits.cpp) - [Linear search using recursion](Recursion/linear-search.cpp) +- [Reverse a number using recursion](Recursion/reverse-a-number.cpp) ## Number System diff --git a/algorithms/CPlusPlus/Recursion/reverse-a-number.cpp b/algorithms/CPlusPlus/Recursion/reverse-a-number.cpp new file mode 100644 index 00000000..884402bc --- /dev/null +++ b/algorithms/CPlusPlus/Recursion/reverse-a-number.cpp @@ -0,0 +1,34 @@ +/* +Description: Program to reverse integer using recursion +Time complexity: O(n) where n is the number of digits in the integer +*/ + +#include +#include +using namespace std; + +int Helper(int n ,int base , int ans) +{ + if(n < 1) + return ans; + ans = ans * base + (n % 10); // Update the ans for every digit + return Helper(n / 10, base , ans); +} + +int Rev(int n) +{ + return Helper(n , 10, 0); +} + + +int main(int argc, char const *argv[]) +{ + cout << Rev(13579); + + return 0; +} +/* +Input: 13579 +Output: 97531 +*/ +