Using Recursion Efficiently

Using Recursion Efficiently

It’s important to use recursion carefully to avoid running into stack overflow. This sample code reverses a given integer number using recursion:

#include "stdafx.h"int myreverse(int num){   static int nLocal = 0;   if(num > 0)     nLocal = (nLocal* 10) + (num%10) ;   else     return nLocal;   return myreverse(num/10);}int main(int argc, char* argv[]){   int number = 0;	   printf("Enter a number: ");   scanf("%d", &number);   number = myreverse(number);   printf("Reverse value: %d

", number);   return 0;}


Share the Post: