dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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