Software Development  >  Basics of Python  >  Code: Finding Factorial

Code: Finding Factorial - Basics of Python - Software Development

Introduction

When working with numbers, it's often necessary to calculate the factorial of a given integer. The factorial of a number is the product of all positive integers less than or equal to that number. In this article, we'll explore how to find the factorial of a number using Python. We'll provide simple code examples and explanations to help beginners understand the concept of factorials and implement them in their programs.

Understanding Factorials

To understand factorials better, let's consider an example. The factorial of 5, denoted as 5!, is calculated as follows:

5! = 5 × 4 × 3 × 2 × 1 = 120

Factorials are widely used in mathematics, statistics, and computer science. They can be used to solve a variety of problems, such as permutations, combinations, and probability calculations.

Calculating Factorial Using a Loop

One way to calculate the factorial of a number is by using a loop. Here's a simple code snippet that demonstrates this approach:

def factorial(n):

    result = 1

    for i in range(1, n + 1):

        result *= i

    return result

number = 5

factorial_result = factorial(number)

print(f"The factorial of {number} is: {factorial_result}")

Code Explanation:

  • We define a function called 'factorial' that takes a parameter 'n' representing the number for which we want to find the factorial.
  • We initialize a variable 'result' to 1. This will store the intermediate results as we multiply the numbers.
  • We use a 'for' loop to iterate from 1 to 'n + 1' (inclusive). The loop variable 'i' represents the current number being multiplied.
  • Inside the loop, we multiply 'result' by 'i' and update the value of  'result'.
  • After the loop, we return the final value of 'result'.
  • We assign a value of 5 to the variable 'result'.
  • We call the 'factorial' function with 'number' as the argument and store the result in 'factorial_result'.
  • Finally, we print the factorial result using an f-string.

Output:

The factorial of 5 is: 120

Calculating Factorial Using Recursion

Another approach to finding the factorial is by using recursion. In recursion, a function calls itself until it reaches a base case. Here's an example of how to calculate factorial using recursion in Python:

def factorial(n):

    if n == 0:

        return 1

    else:

        return n * factorial(n - 1)

number = 5

factorial_result = factorial(number)

print(f"The factorial of {number} is: {factorial_result}")

Code Explanation:

  • We define a function called 'factorial' that takes a parameter 'n' representing the number for which we want to find the factorial.
  • We have a base case where if 'n' is 0, we return 1, as the factorial of 0 is defined as 1.
  • If 'n' is not 0, we recursively call the 'factorial' function with 'n - 1' as the argument and multiply it with 'n'.
  • The function continues to call itself until it reaches the base case.
  • We assign a value of 5 to the variable 'number'.
  • We call the 'factorial' function with 'number' as the argument and store the result in 'factorial_result'.
  • Finally, we print the factorial result using an f-string.

Output:

The factorial of 5 is: 120

Sample Problems and Solutions

Now that we understand how to calculate factorials, let's solve a few sample problems using Python.

Problem 1: Calculate the factorial of a given number 'n'.

def factorial(n):

    result = 1

    for i in range(1, n + 1):

        result *= i

    return result

number = 6

factorial_result = factorial(number)

print(f"The factorial of {number} is: {factorial_result}")

Output:

The factorial of 6 is: 720

Problem 2: Calculate the factorial of the first 'n' natural numbers and display the results.

def factorial(n):

    result = 1

    for i in range(1, n + 1):

        result *= i

    return result

n = 5

for i in range(1, n + 1):

    factorial_result = factorial(i)

    print(f"The factorial of {i} is: {factorial_result}")

Output:

  • The factorial of 1 is: 1
  • The factorial of 2 is: 2
  • The factorial of 3 is: 6
  • The factorial of 4 is: 24
  • The factorial of 5 is: 120

Conclusion

In this article, we explored how to calculate the factorial of a number using Python. We covered two methods: one using a loop and another using recursion. Factorials are essential in various mathematical and computational problems. By understanding the concept and implementing the code examples provided, you should now be able to calculate factorials in Python. Keep practicing and exploring different applications of factorials to strengthen your understanding of this important concept in mathematics and programming.

The document Code: Finding Factorial | Basics of Python - Software Development is a part of the Software Development Course Basics of Python.
All you need of Software Development at this link: Software Development
49 videos|38 docs|18 tests
49 videos|38 docs|18 tests
Download as PDF
Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Download free EduRev App
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

Viva Questions

,

mock tests for examination

,

Semester Notes

,

video lectures

,

Important questions

,

Code: Finding Factorial | Basics of Python - Software Development

,

MCQs

,

past year papers

,

Sample Paper

,

study material

,

practice quizzes

,

Previous Year Questions with Solutions

,

Exam

,

Objective type Questions

,

Code: Finding Factorial | Basics of Python - Software Development

,

Code: Finding Factorial | Basics of Python - Software Development

,

Free

,

Extra Questions

,

Summary

,

pdf

,

shortcuts and tricks

,

ppt

;