Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

What Is Recursion?

The document discusses recursion and provides two examples of using recursion to solve problems. Recursion is when a function calls itself directly or indirectly. It allows certain problems to be solved more easily using recursive algorithms. It is important to include a termination condition to avoid infinite recursion. The examples provided are: 1) A recursive function to calculate the sum of digits in a number. 2) A recursive function to calculate the factorial of a number. Both examples read in a number, call the recursive function, and print the output.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

What Is Recursion?

The document discusses recursion and provides two examples of using recursion to solve problems. Recursion is when a function calls itself directly or indirectly. It allows certain problems to be solved more easily using recursive algorithms. It is important to include a termination condition to avoid infinite recursion. The examples provided are: 1) A recursive function to calculate the sum of digits in a number. 2) A recursive function to calculate the factorial of a number. Both examples read in a number, call the recursive function, and print the output.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

What is Recursion?

The process in which a function calls itself directly or indirectly is called recursion and
the corresponding function is called as recursive function. Using recursive algorithm,
certain problems can be solved quite easily. Recursion involves several numbers of recursive
calls. However, it is important to impose a termination condition of recursion.

1.sum of digit

Step1: Read a Number n

Step2: call sum(n)

Step 3: perform sum of digit operation in recursive function

Step 4: print the sum of the digit

#include <stdio.h>

int sum (int a);

int main()

int num, result;

printf("Enter the number: ");

scanf("%d", &num);

result = sum(num);

printf("Sum of digits in %d is %d\n", num, result);

return 0;

// Recursive function for sum of digit

int sum (int num)

if (num != 0)

return (num % 10 + sum (num / 10)); //Recursive call

else

return 0;

Sample output:
Input :

Enter the number: 123

Sum of digits in 123 is 6

ii) Factorial

Step1: Read a Number n

Step2: call fact(n)

Step 3: perform factorial operation in recursive function

Step 4: print the factorial of the number

#include <stdio.h>

int fact (int);

int main()

int n,f;

printf("Enter the number:");

scanf("%d",&n);

f = fact(n);

printf("factorial of the entered number is = %d",f);

// Recursive Function for Factorial of a number

int fact(int n)

if (n==0)

return 0;

else if ( n == 1)

return 1;

else

{
return n*fact(n-1);

Sample output:

Enter the number:5

factorial of the entered number is = 120

You might also like