0% found this document useful (0 votes)
8 views

Recursion

Uploaded by

wakteshridhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Recursion

Uploaded by

wakteshridhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

SANJIVANI K.B.P.

POLYTECHNIC
Kopergaon, Ahmednagar - 423603

A
PROJECT REPORT
ON
”RECURSION”

Submitted for the subject Programming in ’C’.


Submitted by

Satyavan Somvanshi [202] Rushikesh Sonawane [204]

Wakte Shridhar [231] Ayush Wagh [277]

Under the Guidance of


Prof. V. A. Parjane.
Lecturer, Department of Computer Technology

DEPARTMENT OF COMPUTER TECHNOLOGY

SANJIVANI K.B.P. POLYTECHNIC


KOPERGAON, AHMEDNAGAR - 423603
2021-22
Sanjivani Rural Education Society’s
SANJIVANI K.B.P. POLYTECHNIC
DEPARTMENT OF COMPUTER TECHNOLOGY
2021-22

CERTIFICATE
This is to certify that project report entitled
”RECURSION”

Submitted by

Satyavan Somvanshi [202] Rushikesh Sonawane [204]

Wakte Shridhar [231] Ayush Wagh [277]

Under our supervision and guidance for partial fulfillment of the requirement for
Diploma in Computer Technology affiliated to
Maharashtra State Board of Technical Education, Mumbai
For academic year
2021 - 2022

Prof. V. A. Parjane Prof. G. N. Jorvekar


(Project Guide) (H.O.D)

2
ACKNOWLEDGEMENT

First and the foremost we, express my deep sense of gratitude, sin-
cere thanks and deep sense of appre- ciation to Project Guide Mr. V.A.
Parjane, Department of Computer Technology,Sanjivani K.B.P. Poly-
technic, Kopargaon. Your availability at any time throughout the year,
valuable guidance, opinion, view, comments, critics, encouragement, and
support tremendously boosted this project work.

Lots of thanks to Mr. G. N. Jorvekar, Head Of Department Computer


Technology Department, for providing us the best support we ever had.
We like to express my sincere gratitude to Mr. A.R. Mirikar, Principal,
Sanjivani K. B. P. Polytechnic, Kopargaon for providing a great platform
to complete the project within the scheduled time.

We are also thankful to all the faculty members, Computer Technol-


ogy Department, Sanjivani K. B. P. Polytechnic, Kopargaon for giving
comments for improvement of work, encouragement and help during com-
pletion of the Project.

Last but not the least, we should say thanks from our bottom of heart
to my Family and Friends for their never ending love, help, and support
in so many ways through all this time.

Thank you so much.

3
INDEX

SR.NO. TOPIC PAGE NO.


1). Introduction to Recursion 5
2). What is Recursion in C? 6
3). Advantages and Disadvantages 8
4). Program 9
5). Conclusion 14
6). References 15

4
1. Introduction to Recursion :-

The process in which a function calls itself directly or indirectly


is called recursion and the corresponding function is called as recur-
sive function. Using recursive algorithm, certain problems can be
solved quite easily. Examples of such problems are Towers of Hanoi
(TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph,
etc.

There is a simple difference between the approach (1) and approach(2)


and that is in approach(2) the function “ f( ) ” itself is being called
inside the function, so this phenomenon is named as recursion and
the function containing recursion is called recursive function, at the
end this is a great tool in the hand of the programmers to code some
problems in a lot easier and efficient way.

There is a simple difference between the approach (1) and approach(2)


and that is in approach(2) the function “ f( ) ” itself is being called
inside the function, so this phenomenon is named as recursion and
the function containing recursion is called recursive function, at the
end this is a great tool in the hand of the programmers to code some
problems in a lot easier and efficient way.

5
2. What is Recursion in C? :-

C is a powerful programming language having capabilities like an


iteration of a set of statements ’n’ number of times. The same con-
cepts can be done using functions also. In this chapter, you will be
learning about recursion concept and how it can be used in the C
program. Recursion can be defined as the technique of replicating or
doing again an activity in a self-similar way calling itself again and
again, and the process continues till specific condition reaches. In the
world of programming, when your program lets you call that specific
function from inside that function, then this concept of calling the
function from itself can be termed as recursion, and the function in
which makes this possible is called recursive function
The idea behind recursion is that sometimes a problem is too

problematic or too complex to solve as it is too big. If the problem


can be broken down into smaller forms of itself, we may be able to
find a way to solve one of these smaller forms and then be able to
find a solution to the complete problem.

The function call itself is known as recursive function and the re-
cursion is the process where the function called itself. The recursive
function also referred as self-called function. Whenever a function
called from itself, then there will be an infinite loop. To break this
infinite loop every recursive function must check the terminating con-
dition and then decide whether to call the function again or not. The
example below will show the way of writing this type of function in
C programming.

6
Systax :-

7
3. Advantages and Disadvantages :-

ADVANTAGES :-
1. Recursive processor are easiest to write.
2. Recursive processor are easiest to understand .
3. It is easier to give recursive solution to real life complex prob-
lem rather iterative solutions are tougher or complex . for example:-
Tower of hanoi , Tree Traversal etc.
4. For a recursive function, you only need to define the base case and
recursive case, so the code is simpler and shorter than an iterative
code.
5. Some problems are inherently recursive, such as Graph and Tree
Traversal.
6. The code may be easier to write.
7. To solve such problems which are naturally recursive such as tower
of Hanoi.

DISADVANTAGES :-
1. Recursive procedures are relatively slower than it’s equivalent it-
erative solution.
2. Recursive solutions are occupy more memory as compare to the
equivalent iterative solution.
3. A recursive program has greater space requirements than an iter-
ative program as each function call will remain in the stack until the
base case is reached.
4. It also has greater time requirements because each time the func-
tion is called, the stack grows and the final answer is returned when
the stack is popped completely
5. Recursive functions are generally slower than non-recursive func-
tion

8
4. Program 1 :-

1). Factorial of number using recursion


The factorial of any positive integer or non-negative num- ber x is
equivalent to the multiplication of every integer that is smaller than
this non-negative integer x.
The factorial of a positive number “n” refers to the product of all
the descending integers before it (smaller than the number x). The
factorial of a number is denoted by the symbol “!”. Thus, the facto-
rial of a number will be “number!”. For instance, we will write the
factorial of the non-negative integer x as x!.
Here, x! = x * (x-1) * (x-2) * (x-3) * (x-4) . . . *.
Let us look at an example, 6! = 6x5x4x3x2x1 = 720

We can also write it as: 6! = 6 * 5!


We will pronounce 6! as 6 factorial. As a matter of fact, it is also
known as 6 shriek or 6 bang. We normally utilise factorials in Per-
mutations and Combinations (in Mathematics).
Let us look at a few more examples. 3! = 3x2x1 = 6
5! = 5x4x3x2x1 = 120
7! = 7x6x5x4x3x2x1 = 5040

9
10
11
5. Program 2 :-

2). Greatest Common Divisor using recursion


The greatest common divisor (GCD) of two or more num- bers is
the greatest common factor number that divides them, exactly. It
is also called the highest common factor (HCF). For example, the
greatest common factor of 15 and 10 is 5, since both the numbers
can be divided by 5.
15/5 = 3
10/5 = 2
If a and b are two numbers then the greatest common divisor of both
the numbers is denoted by gcd(a, b). To find the gcd of numbers,
we need to list all the factors of the numbers and find the largest
common factor.
Suppose, 4, 8 and 16 are three numbers. Then the factors of 4, 8
and 16 are:
4 → 1,2,4
8 → 1,2,4,8
16 → 1,2,4,8,16
Therefore, we can conclude that 4 is the highest common factor
among all three numbers.

12
13
6. Conclusion :-

C is a powerful programming language having capabilities like


an iteration of a set of statements ’n’ number of times. The same
concepts can be done using functions also. In this chapter, you will
be learning about recursion concept and how it can be used in the
C program.
Recursion is the process of repeating items in a self-similar way.
In programming languages, if a program allows you to call a function
inside the same function, then it is called a recursive call of the
function.
The C programming language supports recursion, i.e., a function
to call itself. But while using recursion, programmers need to be
careful to define an exit condition from the function, otherwise it
will go into an infinite loop.
Recursive functions are very useful to solve many mathematical
problems, such as calculating the factorial of a number, generating
Fibonacci series, etc.
Recursion can be defined as the technique of replicating or doing
again an activity in a self-similar way calling itself again and again,
and the process continues till specific condition reaches. In the world
of programming, when your program lets you call that specific func-
tion from inside that function, then this concept of calling the func-
tion from itself can be termed as recursion, and the function in which
makes this possible is called recursive function.
C program allows you to do such calling of function within an-
other function, i.e., recursion. But when you implement this recur-
sion concept, you have to be cautious in defining an exit or termi-
nating condition from this recursive function, or else it will continue
to an infinite loop, so make sure that the condition is set within your
program.

14
7. References :-

• Programming in ANSI C Author : Balgurusamy. E

• The C Programming Language Author : Brian, W.


Kernighan, Ritchie Dennis

• Let Us C Author : Kanetkar Yeshawant.

15

You might also like