FOR FREE YEAR SOLVED


Description

The factorial of a number is the product of the integer numbers from 1 to that number. If n is a number then the factorial of n=1×2×3×……×(n-2)×(n-1)×n. The factorial of n is also written as n!

The factorial of 0 is 1. 

For example, 5! = 1×2×3×4×5 = 120.

Algorithm

INPUT: A number.

OUTPUT: Factorial of that number.

PROCESS:

Step 1: [Taking the input]

               Read n [the number to find the factorial]

Step 2: [Calculating the factorial]

               Set f<-1

               For i=1 ton repeat

                              Set f<-f×i

               [End of ‘for’ loop]

               Print "The factorial is: f”

Step 3: Stop.

Code

TIME COMPLEXITY:

for(i=1;i<=n;i++)-------------------------O(n)

                              f=f*i;

The time complexity of finding factorial is O(n) where n is the given number because the product is calculated from 1 to n. 

SPACE COMPLEXITY:

The space complexity is O(1) as it requires a constant number of memory spaces to execute the program for any given input.