Skip to main content

Recursion

Amu_Ke_Fundye

Recursion


A function that calls itself directly or indirectly is called a recursive function. The recursive factorial function uses more memory than its non-recursive counter part. Recursive function requires stack support to save the recursive function calls.
  • Recursion leads to compact
  • It is simple
  • It is easy to understand
  • It is easy to prove correct

Example-1: Factorial Recursive Function


Example-2: GCD Recursive Function

Example-3: Fibonacci Sequence Recursive Function


Example-4: Power Recursive Function (xy)

Example-5: Searching maximum value of an array with function call max(a, 0, n-1).
int mid, leftmax, rightmax;
int max (int a[], int low, int high)
{
if (low == high) return a[low];
else
{
mid = (low + high) / 2;
leftmax = max (a, low, mid);
rightmax = max (a, mid + 1, high);
if (leftmax > rightmax) return leftmax;
else return rightmax;
}
}

Example-6: Computing the sum of numbers from 1 to n
int sum (int n)
{
int s;
if (n == 0) return 0;
s = n + sum(n-1);
return s;
}

Regards 
Amrut Jagdish Gupta

Comments

Popular posts from this blog

Funny Shortcut to Remember Periodic Table

Amu_Ke_Fundye Funny Shortcut to Remember Periodic Table THE PERIODIC TABLE The periodic table is a tabular display of the chemical elements, organized on the basis of their atomic number's , electron configurations, and chemical properties.In order to appreciate the general trends in chemistry and to explain the deviations of some from these general trends we need to to have good knowledge about the position of those elements in periodic table. This knowledge is also extremely important for competitive exams. So, here I present a way to memorize this using some funny easy shortcut sentences. THE FIRST 18 ELEMENTS (H He Li Be B C N O F Ne Na Mg Al Si P S Cl Ar)  H ie  He   Li es  Be cause  B oron  C an N ot  O xide  Fl orine.. N e w  Na tions  M ight  Al so  Si ng  P eaceful  S ong  Cl early  A gain  THE ...

8 Tricks To Win A Game Of Chess

Amu_Ke_Fundye 8 Tricks To Win A Game Of Chess Can you name the game played on a board with 64 squares arranged in an eight-by-eight grid? Here are a few more clues - each player begins the game with 16 pieces and the most powerful piece is the queen and the least powerful piece is the pawn. The game is Chess. It is the game of  strategy and tactics  with players commanding an army of 16 chessmen - pawns, king, queen, bishops, knights, and rooks. The objective is to 'checkmate' the opponent's king by placing it under an inescapable threat of capture. Moreover, a well-played chess game has three stages - opening, middlegame and the final stage. We will let you know more about it, especially the tips and tricks to win a game of chess. 1.  Keep track on your opponent's move : Whenever your opponent makes any move, analyze what made him do so. Is your piece in danger? Strategize as per the moves made by your opponent. 2.  Plan well : If you plan to...

ALU and Data Path

Amu_Ke_Fundye ALU and Data Path The CPU can be divided into a data section and a control section. The   data section , which is also called the   datapath. Datapath The registers, the ALU, and the interconnecting bus are collectively referred to as the datapath.  Each bit in datapath is functionally identical.  The datapath is capable of performing certain operations on data items. The  control section  is basically the  control unit ,  which issues control signals to the datapath . Bus : A Bus is a collection of wires or distinct lines meant to carry data, address and control information. Data Bus : it is used for transmission of data. The number of data lines corresponds to the number of bits in a word. Address Bus : it carries the address of the main memory location from where the data can be accessed. Control Bus : it is used to indicate the direction of data transfer and to coordinate the timing of ...