about-text

SoftOSet is the most remarkable programming site. SoftOSet provides the fresh conceptual and professional programs, articles, books, and other interesting and technical writings. SoftOSet is the most reliable programming site to learn the programs and the basics of the popular programming languages. SoftOSet is the hub of the informative and research based articles.

Recursion Program in C Plus Plus

 

Recursion Program in C Plus Plus

Recursion program is  the another famous program in C++.

Recursion basically means to call itself again and again.

Or in simple words, you can say that

Recursion is the process in which a function call itself again and again. It call itself until a certain breaking condition in it becomes true.

If you have not any knowledge about recursion, then kindly first learn the basic concepts of recursion, and then apply this code.

 

Otherwise, If you know the basic concept of recursion. Then this program is just for you and it will taught you in the soft and simple manner the program of recursion in C++.

 

                                                            Scenario

This program will ask the user to enter the number of which user want to find the factorial. User had entered the number and program outputs him the factorial of the desired number. Also, we have added an additional functionality that it prints the factorials of numbers from 0 to 10 every time user had find a factorial. That’s it.

If you don’ t want to print the factorial of numbers from 0 to 10 then just simply remove the for loop in main function and fact function which is user defined function.

 

                                    Program in C++ of Recursion

Code

 

#include<iostream>

using namespace std;

int rec(int);

            int fact(int);

int main()

{

            int a;

            cout<<"ENTER the number to find Factorial\n";

            cin>>a;

            cout<<"Factorial= \t:"<<rec(a)<<endl;

            for(int counter=0;counter<=10;++counter)

                        cout<<counter<< " ! \t\tFactorial="<<fact(counter)<<endl;

                        return 0;

           

}

int rec(int b)

{

            if(b<=1)

            return 1;

            else

            return b*rec(b-1);

            }

            int fact(int g)

{

            if(g<=1)

            return 1;

            else

            return g*rec(g-1);

}

 

                                        Program in C++ of Recursion


Output

Now, here is the sample output of the recursion program.

 

 

 

 

Program in C++ of Recursion

 

You can see that, In output user wants to find the factorial of 15 and program provide him the factorial.

Also program provide the factorials of numbers from 0 to 10 in advance.

 

****************************        Thank You      *********************************

 


Post a Comment

Feel Free to Comment Us

Previous Post Next Post

Recents