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

Practical 8(Factorial)

The document outlines a practical exercise to calculate the factorial of a number using recursion in both C and Python. It includes the theory behind factorial calculation, an algorithm, and sample code implementations for both programming languages. The program prompts the user for input and handles negative numbers appropriately.

Uploaded by

ajaydhapate16
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)
3 views

Practical 8(Factorial)

The document outlines a practical exercise to calculate the factorial of a number using recursion in both C and Python. It includes the theory behind factorial calculation, an algorithm, and sample code implementations for both programming languages. The program prompts the user for input and handles negative numbers appropriately.

Uploaded by

ajaydhapate16
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/ 4

Practical 8

Aim: Finding factorial of a number using recursion.


Theory:
To find factorial of number three method are given first one uses
for loop, second uses function to find factorial and third using
factorial representation using (!), so 5 factorial.
Written as 5! Factorial as n!
N!=n*(n-1)*(n-2)*(n-3)*…….*3*2*1
And 0!=1
We are going to calculate the factorial of a number using
defining user defined function names as “fact” whose return type will
be long int. We will paas it variable of type int as a parameter whose
factorial it has to find out.
Algorithm:
Step 1: Start

Step 2: Read the input number from the user

Step 2: Declare and initialize variables fact = 1 and i = 1

Step 4: Repeat the loop until i<=num

fact = fact * i

i = i++

Step 5: Print fact to get the factorial of a given number


Step 6: Stop

Flowchart:
Result: In this way we have implemented a program to calculate the
factorial of any given number using recursion.
Name -
PRN-
Branch-

Aim-Finding factorial of a number using recursion using C.

#include <stdio.h>
int factorial(int);
int main()
{
int num;
int result;
printf("Enter a number to find it's Factorial: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
int factorial(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return(num * factorial(num - 1));
}
}

Output:
Name -
PRN-
Branch-

Aim- Finding factorial of a number using recursion using python.

def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)

num = int(input("Enter a number: "))


if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",recur_factorial(num))

You might also like