1055 - Tejasvi Borole FP Assignement-4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Department of Applied Sciences and Engineering

Fundamentals of Programming (Laboratory)

Student Name: Tejasvi Bhanudas Borole.

Class: FY-B.Tech Division: J Roll No: 1055

Semester:2nd Academic Year:2021-22

Subject Name : Fundamental of Programming.


Title of Assignment: Write C program to compute factorial of given positive
integer using recursive function.

Date of Performance:06/06/2022

1)Aim: Write C program to compute factorial of given positive integer


using recursive function.

2)Objectives:
1)To understand and develop C program.
2)To understand concept of editor and compiler.
3)To understand different components of a basic C program.
4)To understand Concept Of operators.

3)Alogarithm:
Step 1:START the program.
Step 2:Declare the function fact(int num).
Step 3:Ask the user to enter the an integer to find the factorial.
Step 4:Read the integer and store it in num1.
Step 5: By using if else we derived many condition and then if num1 is
0 then factorial is 1 and else if num1 is less than 0 then invalid data &
else execute the code .
Step 6:Call the function fact in loop and print factorial of num.
Step 7:Define the function and its operation in function fact and return
the value of fact .
Step 8:Display the value of fact.
Step 9:STOP the program.

4)Flowchart:
START

Declare the num1 and factorial


variables

Read the value of


num1.

NO No
Else if If Else
Num1 Num1==0 num1>0
<0

YES YES
YES

Display Display Factorial=fact(num1)


Invalid factorial is 1 (call the function)

Return Num*fact(num-1)

Num=num+1

Display factorial

5)Program:
#include<stdio.h> STOP
long int fact(int num);
int main()
{
long int num1,factorial;
printf("Enter the number:\n");
scanf("%ld",&num1);
factorial=fact(num1);
printf("The factorial of %ld is %ld",num1,factorial);
return 0;
}

long int fact(int num)


{
int factorial;
if (num==0)
{
return 1;
}
return num*fact(num-1);
}

6)Input and output:


7)Conclusions: In this way we generated code of factorial by using
recursive function in C program. We declare variables num1 and
factorial. we declare function factorial and then call function factorial.
After that we define function factorial double that is called recursive
function if we declare two times function after that return value of fact
and print the value of factorial and display it.this program we did in
gdb compiler online.

You might also like