Explain recursive functions, C/C++ Programming

Assignment Help:

Recursive Functions

Recursion is a process by which a function includes itself with a condition for its safe exit. It is best suitable for a recursive problem. A typical example is the factorial problem, the programs without and with recursive functions are shown below.

void main()

 {

   long factorial ( int num); // Forward declaration of

       //a function

   int num;

printf("\n Enter a number:");

scanf("5d",&num);

printf(" Factorial of 5d is %ld",num,factorial(num));

  }

long factorial(int num)   // Non recursive

 {

   long f=1;

while ( n)

                 {

                   f = f * n;

                   n--;

                 }

                return(f);

 }

long factorial(int num)   // Recursive

 {

                if(n == 1)

                                return (1);

                else

                                return( n* factorial(n-1));

 }

 


Related Discussions:- Explain recursive functions

Recursive functions, a program to determine whether a number is an odd or e...

a program to determine whether a number is an odd or even using recursive function

Functions, what is the difference between call by reference and call by poi...

what is the difference between call by reference and call by pointer method?

Minimum shelfs, Write a program that finds the minimum total number of shel...

Write a program that finds the minimum total number of shelves, including the initial one required for this loading process

#c++ homework, write c++programm that converts degrees Kelvin (TK) to degr...

write c++programm that converts degrees Kelvin (TK) to degrees Fahrenheit(TR) recall that TF=(9/5)TK and that TF=TR-459.67

Explain the standard input/output iostream library, Question 1 Implemen...

Question 1 Implement a class stack which simulates the operations of the stack allowing LIFO operations. Also implement push and pop operations for the stack 2 Explain the c

What happens if you write following code?, What happens if you write follow...

What happens if you write following code? string& foo()

Bulk listing posting for a site like ebay, Project Description: I posted...

Project Description: I posted listings on a site that is like eBay but for video games. I post the similar listing many times daily, but need something that will let me post

Some of the basic rules of cpp program, Ba s i c r u l e s o f C...

Ba s i c r u l e s o f C + + p r o g r a m : ·     I t m u s t h a v e o n l y o n e m a i n f u n c ti o n ·

Implement binary heap in c++?, A:BinaryHeap.h ------------ #ifndef BI...

A:BinaryHeap.h ------------ #ifndef BINARY_HEAP_H_ #define BINARY_HEAP_H_ #include "dsexceptions.h" #include "vector.h" // BinaryHeap class // CONSTRUCTION: wi

What is difference between require_once () and require(), What is differenc...

What is difference between require_once (), require(), include()? require() includes and evaluates a specific file, if file isn't found then it shows a Fatal Error. require_

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd