0% found this document useful (0 votes)
3 views4 pages

Java 27

The document provides an introduction to recursion, explaining its definition, purpose, and components such as halting conditions and recursive calls. It includes examples of recursive functions in Java to print natural numbers in ascending and descending order. Additionally, it emphasizes the importance of base statements and recurrence statements in the functioning of recursive functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Java 27

The document provides an introduction to recursion, explaining its definition, purpose, and components such as halting conditions and recursive calls. It includes examples of recursive functions in Java to print natural numbers in ascending and descending order. Additionally, it emphasizes the importance of base statements and recurrence statements in the functioning of recursive functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lesson:

Recursion
Pre-Requisites
Functions

List of Concepts Involved


Introduction to recursive function
Working rules of recursive function
Write a program to print all natural numbers from 1 to n using recursion
Write a program to print all natural numbers from n to 1 using recursion.

Topic 1: Introduction to recursive functions


Let's say if someone asks you to calculate 5! , you can say give me 4! and I will just multiply it to 5 and return it
to you then the other person will say that no, you give me 3! and then I will multiply it by 4 to give you 4! and so
on...

In this example everyone is doing the same task . A recursive function works in the same way.

More formally, a recursive function is a function that calls itself again and again until certain conditions are
met. It has basically two parts
A precondition that is used to stop this recursive call (Halting Condition)
A function that is capable of calling itself ( recursive call).

Halting Condition:

Just like how we have a condition in an iterative statement to terminate the loop similarity, it must have a
halting condition to terminate the recursive call; otherwise, it will result in an overflow error as it will go inside an
infinite recursive call.

Why Do We Need Recursion?

Recursion is generally used when dealing with complex problems and problems that form a hierarchical
pattern; it solves the original problem breaking into the smaller subproblems. Recursion is inefficient in cases
where the same value is calculated again and again. It requires extra memory on the stack for each recursive
call.

Topic 2: Working of Recursive functions


A recursive function has 3 parts
Base statement - It is the statement at which the recurrence will terminate. It is generally a condition for
which the solution is known or can be calculated easily. Without it the recurrence will continue forever
Recurrence statement - It is the statement which calls the function again
The rest of the function where we do our computations.

Cracking the Coding Interview in JAVA - Foundation


Syntax

In Java we can write a recursive function using the following syntax:

methodName (N parameters )

if(haltCondition){

return result

return methodName (N parameters )

Example -

Problem 1 Write a program to print all natural numbers from 1 to n using recursion.

Input n=5

Output 1 2 3 4 5

Approach
We define a function func that accepts a number N
We recursively call the function and print the value of N, until the value of N is greater than [Link] is the base
condition for this recursive call
We check if the value of N is greater than 0 then we call func(N-1) and then print the value of N.

Code Link [Link]

Problem 2 Write a program to print all natural numbers from n to 1 using recursion.

Input n=5

Output 5 4 3 2 1

Approach
We define a function func that accepts a number N
We recursively call the function and print the value of N, until the value of N is greater than [Link] is the base
condition for this recursive call
We print the value of N then we check if the value of N is greater than 0 if so then we call func(N-1).

Code Link [Link]

Cracking the Coding Interview in JAVA - Foundation


Upcoming Class Teasers
Problems based on Recursion.

Cracking the Coding Interview in JAVA - Foundation

You might also like