-4

here is my code -

#include<bits/stdc++.h>
#define ll long long
using namespace std;

void printno(ll n){
    if(n==1){
        cout<<1<<endl;
        return;
    }
    printno(n-1);
    cout<<n<<"\t";
}
int main(){
 ll n;
 cin>>n;
 printno(n);
return 0;
}

i am using vs code. and my code not working for input greater than 10^5. what is the problem in my code? and how can i get output for input greater than 10^7.

  • 3
    To deep recursion results in stack overflow. – 273K Dec 12 '22 at 06:30
  • 3
    The first 3 lines of your code are considered bad practice. Where are you learning C++ from? Use `#include `, remove the `#define` (avoid preprocessor in C++). And remove `using namespace std` from your code. There should be no valid reason to use `long long` use `unsigned int` or `std::uint64_t` (from header). If you want to know more search SO for stdc++.h, using namespace std. – Pepijn Kramer Dec 12 '22 at 06:52
  • 2
    (1) About `#include`: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h, (2) About `using namespace std;`: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice. – wohlstad Dec 12 '22 at 07:28
  • If you have to use recursion, there are compiler+linker command line switches that will increase the available stack space. If you want recursion up to a million, you'll probably need at least 35MB of stack space. There's no free lunch. – selbie Dec 12 '22 at 07:34
  • You can replace your `#define` by a `using` directive instead if you really want it. It's better and safer. – Fareanor Dec 12 '22 at 10:19

1 Answers1

2

You probably ran out of stack space on your platform by trying to have more than 100,000 recursive calls on the stack at once. The website we are on is named after the thing that happened to your program. I'm not sure what your end goal is here, but try to come up with a way to do it without recursion or without recursing so many times (e.g. use a simple for loop).

David Grayson
  • 84,103
  • 24
  • 152
  • 189