P. 1
CM304

# CM304

|Views: 10|Likes:

See more
See less

03/18/2014

pdf

text

original

# DEPARTMENT OF TECHNICAL EDUCATION ANDHRA PRADESH

Name : Murali Krishna Chintala : Lecturer in CME : Computer Engineering : SUVR & SR GPW, Ethamukkala : III Semester : Unix & C : CM – 304 : Understand Modular Programming : 50 Min : Recursion. : PPT, Animations
CM304.68 1

Designation Branch Institution Year/ Semester Subject Name Subject Code Major Topic Duration Sub Topic Teaching Aids

Objective

On completion of this period, you would be able to know :
• Understand the concept of Recursion.

CM304.68

2

Recursion

• A function is recursive if a statement in the body of the function calls itself. • It is the process of defining something in terms of itself.

CM304.68

3

Recursive function is • Clear. • Short. • Contains simple programs.

CM304.68

4

Contd..

• Commonly used example of a recursive procedure is finding the factorial of a number. • Factorial of number N is defined as N! = N*(N-1)*(N-2)*……..1

CM304.68

5

Contd..

• Recursive definition should have a stopping condition i.e., 0! =1. • Complete definition of factorial function is N! = N*(N-1)! with 0! = 1
CM304.68 6

Program for factorial of a number
#include<stdio.h> int fact(int); main() { int n; scanf(“%d”,&n); printf(“factorial of number %d is %d\n”,n,fact(n)); }

CM304.68

7

Program for factorial of a number int fact (int x) { int f =1; if(x==1) return(1); else if(x>0) f=x*fact(x-1); return(f); }
CM304.68

contd,..

8

Program to find sum of digits of a number
#include<stdio.h> int sumdigit(int); main() { int n; scanf(“%d”,&n); printf(“sum of digits is %d:”,sumdigit(n)); }
CM304.68 9

Program to find sum of digits of a number
int sumdigit(int x) { int sum =0; if(x==0) return(sum); else sum=(x%10 + sumdigit(x/10)); return(sum); }
CM304.68

Contd..

10

Summary
In this class, we have learnt about.. • A function calling itself is known as recursion.

CM304.68

11

Quiz
1. A Function calls itself is called.. a. Recursion b. Excursion c. None

CM304.68

12

Quiz
1. A Function calls itself is called.. a. Recursion b. Excursion c. None

CM304.68

13

Quiz
2. Recursion program is.. a. Short b. Simple c. Both

CM304.68

14

Quiz
2. Recursion program is.. a. Short b. Simple c. Both

CM304.68

15