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

Chap03 Recursion Programs

This document covers the topic of recursion in programming, including sample programs for calculating the sum of the first N numbers, the factorial of a number, the GCD of two numbers, and generating the Fibonacci series. Each section includes code snippets and explanations of the recursive functions. The document serves as an educational resource for understanding recursion through practical examples.

Uploaded by

sannakkiyukta
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)
8 views4 pages

Chap03 Recursion Programs

This document covers the topic of recursion in programming, including sample programs for calculating the sum of the first N numbers, the factorial of a number, the GCD of two numbers, and generating the Fibonacci series. Each section includes code snippets and explanations of the recursive functions. The document serves as an educational resource for understanding recursion through practical examples.

Uploaded by

sannakkiyukta
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
You are on page 1/ 4

Chapter - III

Recursion

Syllabus
• Factorial of a number

• GCD of two numbers

• Ackerman’s problem

• Fibonacci series

Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 1 of 4


Chapter-03 Sample Programs, Recursion

1 Sum of First N Number


1 # include < stdio .h >
2
3 int addNumbers ( int n ) ;
4
5 int main () {
6
7 int num ;
8 printf ( " \ nEnter a positive integer > " ) ;
9 scanf ( " % d " , & num ) ;
10
11 printf ( " \ nSum = % d " , addNumbers ( num ) ) ;
12
13 return 0;
14 }
15
16 int addNumbers ( int n ) {
17
18 if ( n == 0)
19 return n ;
20 else
21 return n + addNumbers ( n - 1) ;
22
23 }

2 Factorial of a Number
1 # include < stdio .h >
2
3 // fibonacci () funtion definition
4 int fibonacci ( int num )
5 {
6 // first base condition check
7 if ( num == 0)
8 {
9 return 0;
10 }
11 // second base condition check
12 else if ( num == 1)
13 {
14 return 1;
15 }
16 // else calling the fibonacci () function recursively till we get to the
base conditions
17 else
18 {
19 return fibonacci ( num - 1) + fibonacci ( num - 2) ;
20 }
21 }
22
23 int main ()
24 {
25 // variable to store how many elements to be displayed in the series
26 int num ;
27

28 int i ;
29
30 // taking user input
31 printf ( " \ nEnter the number of elements to be in the series > " ) ;

Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 2 of 4


Chapter-03 Sample Programs, Recursion

32 scanf ( " % d " , & num ) ;


33
34 printf ( " \ nThe series is >\ t " ) ;
35 // calling fibonacci () function for each iteration and printing the
returned value
36 for ( i = 0; i < num ; i ++)
37 {
38 printf ( " %d , " , fibonacci ( i ) ) ;
39 }
40
41 return 0;
42 }

3 GCD of Two Numbers


1 # include < stdio .h >
2
3 int gcd ( int n1 , int n2 ) ;
4
5 int main () {
6 int n1 , n2 ;
7
8 printf ( " Enter two positive integers > " ) ;
9 scanf ( " % d % d " , & n1 , & n2 ) ;
10
11 printf ( " G . C . D of % d and % d is % d . " , n1 , n2 , gcd ( n1 , n2 ) ) ;
12 return 0;
13 }
14
15 int gcd ( int n1 , int n2 ) {
16
17 if ( n2 != 0)
18 return gcd ( n2 , n1 % n2 ) ;
19 else
20 return n1 ;
21
22 }

4 Fibonacci Series
1 # include < stdio .h >
2

3 int factorial ( int n )


4 {
5 if ( n == 0 )
6 return 1;
7 else
8 return ( n * factorial (n -1) ) ;
9 }
10
11 int main ()
12 {
13 int number ;
14 int fact ;
15
16 printf ( " \ nEnter a number > " ) ;
17 scanf ( " % d " , & number ) ;
18
19 fact = factorial ( number ) ;

Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 3 of 4


Chapter-03 Sample Programs, Recursion

20
21 printf ( " \ nFactorial of % d is % d \ n " , number , fact ) ;
22
23 return 0;
24 }

Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 4 of 4

You might also like