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

Kantipur Engineering College: Lab Report 5

The document is a lab report that discusses recursion. It defines recursion as repeating items in a self-similar way and provides examples of recursive functions in programming languages. The report discusses key concepts of recursion like base cases, memory allocation, and using recursion to solve problems like checking for palindromes, calculating prime numbers, and converting decimals to binaries. It concludes that recursion helps solve problems efficiently by reducing unnecessary function calls and making programs more understandable.

Uploaded by

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

Kantipur Engineering College: Lab Report 5

The document is a lab report that discusses recursion. It defines recursion as repeating items in a self-similar way and provides examples of recursive functions in programming languages. The report discusses key concepts of recursion like base cases, memory allocation, and using recursion to solve problems like checking for palindromes, calculating prime numbers, and converting decimals to binaries. It concludes that recursion helps solve problems efficiently by reducing unnecessary function calls and making programs more understandable.

Uploaded by

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

Kantipur Engineering College

Lab report 5

C lab report submitted by :

Name : prabesh raj upadhaya

Roll no : 21

Lab Date: 2078/03/15

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.

Base condition in recursion :


In the recursive program, the solution to the base case is provided and the solution of
the bigger problem is expressed in terms of smallerproblems.
int fact (int n){
if (n=1) // base case.
return 1;
else
return n* Fact (n-1);
}
In the above example, base case for n <= 1 is defined and large value of number can
be solved by converting to smaller one till base case us reached.

Memory allocation of Recursive method:


Each recursive call creates a new copy of that method in the memory. Once some data
is returned by the method, the copy is removed from the memory. Since all the
variables and other stuff declared inside function get stored in the stack, therefore a
separate stack is maintained at each recursive call. Once the value is returned from
the corresponding function, the stack gets destroyed. Recursion involves so much
complexity in resolving and tracking the values at each recursive call. Therefore we
need to maintain the stack and track the values of the variables defined in the
stack.Let us consider the following example to understand the memory allocation of
the recursive functions.
int display (int n)
{
if(n == 0)
return 0; // terminating condition
else
{
printf("%d",n);
return display(n-1); // recursive call
}
}

1)Write a program to check whether the number entered by the user is a


palindrome or not using recursive function.
Algorithm:
Algorithm for main Function: int main ( )
Step 1: Start
Step 2: Declare variables n, pal.
Step 3: Read the value of ‘n’.
Step 4: Call palin(n) function as rev = palin(n).
Step 5: Check whether n=rev?
If true, Print NO. is a palindrome’, and go to step 6.
If false, Print NO. is not palindrome’, and go to step 6.
Step 6: Stop.

Algorithm for recursive function: int palin(int num)


Step 1: Declare static variable rev.
Step 2: Assign value of static variable rev = 0.
Step 3: Check whether num != 0?
If true, calculate rem=num%10
rev=rev*10+rem and then call palin(num/10) function. If false, go to step 4.
Step 4: return rev.
Source code:
#include <stdio.h>
#include <stdlib.h>
int palin(int num)
{
static int rev=0,rem;
if(num!=0)
{
rem=num%10;
rev=rev*10+rem;
palin(num/10);
}
return rev;
}
int main()
{
int n,rev;
printf("Enter a number: ");
scanf("%d", &n);
rev= palin(n);
if (n==rev)
printf("%d is a palindrome",n);
else
printf("%d is not palindrome",n);
return 0;
}
Output:
Enter a number: 121
121 is a palindrome
Flowchart:
2)Write a program to print the series of prime numbers up to the range entered by
the user using recursive function.
Algorithm:
For Calling function int main ( )
Step 1: Start
Step 2: Declare variables n, a.
Step 3: Read the n.
Step 4: Assign a=2
Step 5: Check a<=n?
If true, go to 6 If false, go to 7
Step 6: Check prime(2,a)==0? by calling prime(2,a) If yes, Print a
If no, go to 7
Step 7: Stop.
For Called function prime(int a,int num)
Step 1: Start
Step 2: Is num==a?
If yes return 0. If no go to 3
Step 3: If num % a==0?
If yes return 1 If no go to 4
Step 4: return prime(a+1,num).
Step 5: Stop.
Source code:
#include <stdio.h>
#include <stdlib.h>
int prime(int a, int num)
{
if (num==a) return 0;
else
if(num%a==0) return 1;
else
{
return prime(a+1,num);
}
}
int main()
{
int n,a;
printf("Enter the number: ");
scanf("%d",&n);
printf("prime between 1 to n: ");
for(a=2;a<=n;a++)
if(prime(2,a)==0)
printf("%d ",a);

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.

You might also like