Page 409 - Computer science 868 Class 12
P. 409
Recursion Iteration
Fresh memory space is allocated for each recursive call. It uses the same memory locations for variables and
repeats the same unit of code.
It is a slow process. It is faster than recursion.
11.3 SIMPLE RECURSIVE METHODS
The following points should be kept in mind while designing a recursive method.
1. Identifying the exit case. Single exit case called to base case to stop calling recursive methods. There may be multiple
exit cases also depending on the requirement of the problem.
2. Determining the change in actual parameter of the recursive case and the operation to be performed on the other
variables.
3. In recursive methods on numbers which involve digit extraction has base case reached when the number becomes
0. In recursive method call we have to divide the actual parameter by 10 to get the new number.
4. In other recursive methods involving general numbers, the base case is reached either when the parameter becomes
0 or it becomes greater than the number. The recursive method call involves increasing or decreasing the parameter
by 1.
5. In case of array the base case will be, reaching the end of the array that is the index position becoming equal to the
length of the array. In recursive cases the array index changes, the most common form is increasing by 1.
6. While designing a recursive method to handle string, the base case is, either the index position becoming equal to
the length of the String or -1 or the String becomes null.
Let us study simple recursive methods with related questions.
11.3.1 Solved Programs Using Recursive Methods
Program 4 Design a class Pronic to check if a given number is a pronic number or not. [A number is said
to be pronic if the product of two consecutive numbers is equal to the number] Example:
0 = 0 × 1, 2 = 1 × 2, 6 = 2 × 3, 1 2 = 3 × 4; thus, 0, 2, 6, 12... are pronic numbers. Some of the
members of the class are given below: [ISC Specimen Paper 2023]
Class name : Pronic
Data Members/Instance variables
num : to store a positive integer number
Methods/Member functions
Pronic() : default constructor to initialise the data member with
legal initial value
void acceptnum() : to accept a positive integer number
boolean ispronic(int v) : returns true if the number ‘num’ is a pronic number,
otherwise returns false using recursive technique
void check() : checks whether the given number is a pronic number
by invoking the function ispronic() and displays the
result with an appropriate message
Specify the class Pronic giving details of the constructor(), void acceptnum(), boolean
ispronic(int) and void check(). Define a main() function to create an object and call the
functions accordingly to enable the task.
1 import java.util.*;
2 class Pronic
407
Recursion 407

