From e786cd5fefd59d26a8d991e6c1904a978df055c5 Mon Sep 17 00:00:00 2001 From: Shikha Date: Fri, 1 Oct 2021 23:45:29 +0530 Subject: [PATCH] recaman's sequence --- C++/recaman's sequence.cpp | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 C++/recaman's sequence.cpp diff --git a/C++/recaman's sequence.cpp b/C++/recaman's sequence.cpp new file mode 100644 index 00000000..206fef38 --- /dev/null +++ b/C++/recaman's sequence.cpp @@ -0,0 +1,41 @@ +// Cpp program to print n-th number in Recaman' sequence +#include +#include +using namespace std; + +int recaman(int n) +{ + + int arr[n]; + + arr[0] = 0; + printf("%d, ", arr[0]); + + for (int i=1; i< n; i++) + { + int curr = arr[i-1] - i; + int j; + for (j = 0; j < i; j++) + { + if ((arr[j] == curr) || curr < 0) + { + curr = arr[i-1] + i; + break; + } + } + + arr[i] = curr; + printf("%d, ", arr[i]); + } + return 0; +} + +// Driver code +int main() +{ + int n; + cout<<"Enter the n-th number: "; + cin>>n; + recaman(n); + return 0; +}