0% found this document useful (0 votes)
42 views7 pages

(A Constituent College of Somaiya Vidyavihar University) : Page No PIC Sem I/August-December 2020

This document describes a virtual lab experiment on calculating factorials using recursion. The program prompts the user to input a number and uses a recursive function to calculate its factorial. It provides the program code, sample outputs for inputs 5 and 20, and conclusions. It also answers two descriptive questions about base conditions in recursion and memory allocation for recursive function calls.

Uploaded by

Vaibhav Gala
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)
42 views7 pages

(A Constituent College of Somaiya Vidyavihar University) : Page No PIC Sem I/August-December 2020

This document describes a virtual lab experiment on calculating factorials using recursion. The program prompts the user to input a number and uses a recursive function to calculate its factorial. It provides the program code, sample outputs for inputs 5 and 20, and conclusions. It also answers two descriptive questions about base conditions in recursion and memory allocation for recursive function calls.

Uploaded by

Vaibhav Gala
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/ 7

K. J.

Somaiya College of Engineering, Mumbai-77


(A Constituent College of Somaiya Vidyavihar University)

Batch: A1 Roll No.: 16010120016

Experiment / assignment / tutorial No. 7

Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

TITLE: Virtual Lab experiment on recursion

AIM: Virtual Lab experiment on recursion

http://cse02-iiith.vlabs.ac.in/

http://cse02-iiith.vlabs.ac.in/exp9/simulation/index.html

Program to find factorial of a number using recursion


______________________________________________________________________
Expected OUTCOME of Experiment:
CO1.Formulate a problem statement and develop the logic (algorithm/flowchart) for its
solution.
CO2. Apply basic concepts of C programming for problem solving.
CO3. Illustrate the use of derived and structured datatypes such as arrays, strings,
structures and unions.
CO4. Design modular programs using functions and demonstrate the concept of
pointers and file handling

_____________________________________________________________________
Books/ Journals/ Websites referred:

1. Programming in C, second edition, Pradeep Dey and Manas Ghosh, Oxford


University Press.
2. Programming in ANSI C, fifth edition, E Balagurusamy, Tata McGraw Hill.
3. Introduction to programming and problem solving , G. Michael Schneider
,Wiley India edition.
4. http://cse.iitkgp.ac.in/~rkumar/pds-vlab/

_____________________________________________________________________

Department of Science and Humanities

Page No PIC Sem I/August-December 2020


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)

Problem Definition:
The Program prompts user for entering any integer number, finds the factorial of
input number and displays the output on screen. Use a recursive user defined
function to perform the task. A function find_factorial that calls itself in a
recursive manner to find out the factorial of input number.

Flowchart:

Department of Science and Humanities

Page No PIC Sem I/August-December 2020


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)

Implementation details:
#include <stdio.h>
int find_factorial (int);
int main()
{
int n, factorial;
//prompts user to enter any integer
printf("\n Enter any positive integer: \n");
scanf("%d",&n);
factorial=find_factorial (n);
//print the value of factorial
printf("\n Factorial = %d",factorial);
return 0;
}
//calling recursive user defined function
int find_factorial (int num)
{
if(num == 0)
{
return 1;
}
else
{
return num * find_factorial (num-1);
}
}

Department of Science and Humanities

Page No PIC Sem I/August-December 2020


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)

Output for factorial :-

Output(s):

(Attach screenshots of the Output of Program Code implemented in Virtual Lab and
Quiz attempted)

Output :-

for n=5

Department of Science and Humanities

Page No PIC Sem I/August-December 2020


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)

for n=20

Department of Science and Humanities

Page No PIC Sem I/August-December 2020


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)

Department of Science and Humanities

Page No PIC Sem I/August-December 2020


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)

Conclusion:
Hence we wrote a program to find factorial of a number using recursion.

Post Lab Descriptive Questions

1. What is base condition in recursion?


Ans). We see that if the number n is less than or equal to 1, we return 1 instead of a
recursive call. This is called the base condition/case for the factorial which allows
stopping the recursion. Hence, the base condition basically decides how many times a
recursive function should call itself.

2. How memory is allocated to different function calls in recursion?


Ans). When any function is called from main(), the memory is allocated to it on the
stack. A recursive function calls itself, the memory for a called function is allocated
on top of memory allocated to calling function and different copy of local variables
is created for each function call.

Date: 06/12/2020 Signature of faculty in-charge

Department of Science and Humanities

Page No PIC Sem I/August-December 2020

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy