You are on page 1of 5

RAJSHAHI UNIVERSITY OF ENGINEERING & TECHNOLOGY

Department of Electronics & Telecommunication Engineering

LAB REPORT

Course Code : CSE-1154


Course Title : Sessional based on CSE-1153
Experiment No : 06
Experiment Name : Experimental study on C programming language using
recursive function and storage class
Date of Experiment : 04.11.2020
Date of Submission : 10.11.2020

Submitted By: Submitted To:


Roll: 32, 33, 34, 35, 36, 37, 38, 39, 40 Hasan Sarker
Year: 1st year (ODD) Lecturer
Dept. of ETE, RUET

Page 1 of 5
Objectives: The main objectives of this experiment are

1. To learn about recursion in a function

2.  To write program using recursion

3. To know the types of storage classes in C programming language

4. To understand the use of storage classes in a function

Theory: Recursion is a process by which a function calls itself repeatedly, until some specified
condition has been satisfied. Recursion makes the code simpler and shorter than an iterative one.
Again recursive function need more memory allocation than iterative one. Two conditions must
be satisfied to solve a problem recursively. First, the problem must be written in a recursive
form. Second, the problem statement must include a stopping condition.

Figure 1 : Flowchart showing recursion

Storage Class: A storage class defines the scope and life-time of variables in a program. There
are 4 different storage classes in a C program.

1. Auto Storage Class: The auto storage class is the default storage class for all local
variables in a function. Keyword auto is used to define auto storage class.
2. Register Storage Class: The register storage class is used to store the variable in CPU
registers rather than memory location for quick access. Keyword register is used to
represent register storage class.

Page 2 of 5
3. Static Storage Class: The static storage class are used for both local and global
variables. For static variables, memory is allocated only once and storage duration
remains until the program terminates. Static variables are defined with keyword static.
4. Extern Storage Class: Extern storage class is used when we have global functions or
variables which are shared between two or more files. Keyword extern is used to declare
a global variable in another file to provide the reference of variable which have been
already defined in the original file.

Software used in lab: Codeblocks

Experimental Study:
Problem 1 : C program to find the factorial of a natural  number using recursion.

Sample Code:

#include<stdio.h>
int fact(int a)
{
if(a<=1) return 1;
else
{
return (a*fact(a-1));
}
}
int main()
{
int i;
printf(" Enter the number: ");
scanf("%d",&i);
printf(" Factorial of %d is = %d\n",i,fact(i));

Output for problem 1:

Figure 2:  Output of the factorial of a natural number.

Page 3 of 5
Problem 2 : C program to find the factorial of first n natural numbers using recursion and
summation of them.

Sample Code:

#include<stdio.h>

int fact(int a)

if(a<=1) return 1;

else

return (a*fact(a-1));

main()

int b,c,num;

long long int sum=0;

printf("Input a number: ");

scanf("%d",&num);

for(b=0 ; b<=num ; b++)

c=fact(b);

sum+=c;

printf(" %d! = %d\n",b,c);

printf(" Summation of the numbers = %lld",sum);

}
Page 4 of 5
Output for problem 2:

           Figure 3:  Output of the factorial of first n natural numbers and their summation

Discussion: In the experiment we have learnt how to find the factorials of natural numbers using
recursion. We have used a base condition to stop the recursion. And we have used long long
integer data type for sum variable. We have carefully written the code and used semicolon after
ending every statement.

Conclusion: Recursion is useful for repetitive computations in a program. It makes the program
elegant and easy to understand. Also time complexity can also be reduced by using recursion. So
we should properly learn recursion to write an efficient program.

Page 5 of 5

You might also like