Class 12 ISC- Java

Class 12th Java aims to empower students by enabling them to build their own applications introducing some effective tools to enable them to enhance their knowledge, broaden horizons, foster creativity, improve the quality of work and increase efficiency.
It also develops logical and analytical thinking so that they can easily solve interactive programs. Students learn fundamental concepts of computing using object oriented approach in one computer language with a clear idea of ethical issues involved in the field of computing
Class 12th java topics includes revision of class 11th, constructors, user-defined methods, objects and classes, library classes , etc.


Recursion

Recursion occurs when a function calls itself. In Java recursion is used as a form of repetition that does not involve iteration. This technique provides a way to break complicated problems down into simple problems which are easier to solve.

Recursive Function



When a function call itself again and again it is known as recursive function and this concept is called recursion. Before you start working with recursive function you must know that every recursive function must have at least two cases :

  • Base case : It is a condition in recursive function which is used to stop the recursion.

  • Recursive case : It is a statement which is used to further call the function within the body. In other words it is the more general case of the problem we are trying to solve using recursive function.


Infinite Recursion



A recursive function in which base case is not present due to which function calls itself infinitely is known as infinite recursion.


Finite Recursion



A recursive function in which base case is present due to which function calls itself finitely is known as finite recursion.


Direct Recursion



Recursive function in which it calls itself directly is known as direct recursion.

For Example :

direct recursion


Indirect Recursion



When a function calls itself indirectly through another function it is known as indirect recursion

For Example :

indirect recursion

An example of to calculate and return factorial of a no.

factorial using recursion


Output :

 output of factorial using recursion



Recursive function Working




Let us see how recursion works by an example show below:

n

In the above example, we have called function named as b in void main,this is said to be a normal call method

And when we are again and again calling the same b() function,then it is said to be recursive call.So b() is a recursive method, that is a method which invoke itself again & again

Computing factorial by recursion




A famous example often used to describe the recursion in java is the calculation of factorial.
Factorial of a non-negative number is defined as product of all values from 1 to the number itself

Let use see the program to calculate the factorial by recursion function:



Output:

Factorial of number 5 is:120


In the above example, we can observe that the factorial() method is called from the main() method, with a number passed in an argument so it is said to  be direct call method.

And, the factorial method is calling itself again and again so it is said to be a recursive call method. During the direct method call, we passed the value to the factorial method as 5. In the next, recursive call,4 is passed to the factorial() method.This process continues until n becomes 1.

When n will be equal to 1, then if statement returns false and hence 1 is returned. Finally, the collective result of the  factorial() method is passed to the main method

Let us see the working by a diagram:

Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com