dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Intermediate
Jun 7, 2005

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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\n\n", number);
   return 0;
}
Sudharsan Srinivasan
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date