Kantipur Engineering College: Lab Report 5
Kantipur Engineering College: Lab Report 5
Lab report 5
Roll no : 21
SIGN
TITLE: RECURSIVE FUNCTIONS
OBJECTIVE
To get familiarized with the basic concept of recursion and recursive function.
THEORY:
RECURSION:
Recursion is the process of repeating items in a self-similar way. In programming
languages, if a program allows us to cay a function inside the same function, then it is
called a recursive call of the function.
syntax:
void recurion () {
recursion (); /* Function calls 1+ self */
}
int main () {
recussion ()
Recursive functions are very useful to solve many mathematical problems, such as
calculating the factorial of a number, generating Fibonacci series.
Output:
Enter the number: 20
prime between 1 to n: 2 3 5 7 11 13 17
Flowchart:
4)WAP to convert the decimal number given by the user into binary using recursion.
Algorithm:
Algorithm for binary function:
Step 1: Declare function binary and local variables num, con and bin.
Step 2: check if num-con ==0. If true then returnO, else go to step 3.
Step 3: Evaluate bin as:
bin= num- con % 2 + 10% binary (num, con % 2);
and return bin.
Algorithm for main Function:
Step 1: Start
Step 2: Declare variables deci and bin=0
Step 3: Read deci
step 4: Call function binary (deci) and store the value of binary (deci) to bin
Step5: Display bin
Step 6: stop
Source code:
#include <stdio.h>
int binary (int);
int main() {
int deci, bin=0,;
print("enter a decimal number: ");
Scanf("%.d, & deci);
bin = binary (deci);
printf("The binary equivalent of %d is: %d", deci, bin);
return 0;}
int binary (int num,con)
{
int bin;
if (num-con ==0){
return 0;}
else{
bin = (num. con% 2+10 * binary (num.con/2));
return bin;}}
OUTPUT:
Enter a decimal number: 10
The binary equivalent of 10 is: 1010
Flowchart:
3)Write a program to read a multi-digit integer and calculate the sum of digits upto
one digit using recursion.
Algorithm:
For Calling Function int main ( )
Step 1: Start
Step 2: Declare variables num,sum.
Step 3: Read num.
Step 4: Call sumofdigit(num) function as sum = sumofdigit(num).
Step 5: Call sumof digit (sum) function as sum = sumofdigit(sum)
Step 6: Check whether sum > 9?
If true, go to step 5. If false, go to step 7.
Step 7: Print the sum of digits
Step 8: Stop.
For Called Function int sum(int n)
Step 1: Start.
Step 2: Check whether num= 0?
If true, return 0.
If false, return ((num%10)+ sumofdigit(num/10)).
Step 3: Stop
Source code:
#include <stdio.h>
#include <stdlib.h>
int sumofdigit(int n);
int main()
{
int num,sum;
printf("Enter any number: ");
scanf("%d",&num);
sum=sumofdigit(num);
do
{
sum = sumofdigit(sum);
}
while (sum > 9);
printf("The sum of digits is %d", sum);
return 0;
}
int sumofdigit(int num)
{
if(num==0) return 0;
return ((num%10)+ sumofdigit(num/10));
}
Output:
Enter any number:12
The sum of digits is 3
Flowchart:
DISCUSSION AND CONCLUSION:
From this lab session, we learned about the Concept and uses of recursion
function. We learned about the structures of recursive function and learned how
to create it, call it and use it in a program. We find that using recursive function
reduces unnecessary calling of function. Through Recursion one can solve
problems in easy way while it's iterative solution is very big and complex.
In this function lab, we learnt to check the given number is palindrome or not, to
find the sum of multi digits number less than one digit, to print the prime
numbers up to the required range and we also were able to perform decimal to
binary calculation using recursive function.
Using of recursion makes program quite understandable. The programs done in
recursive can also be done by using iterative function but using recursive, the
maximum loop is reduced. As the function is calling itself.
Hence, this lab session became very useful for us and helps us to create a
recursive function.