🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Calculating the factorial of a number is a fundamental problem in both mathematics and computer science. In programming, factorials can be calculated using various methods, and here we will focus on using a while loop in Python.
The factorial of a non-negative integer n is the product of all positive integers less than or equal to n, commonly denoted as n!. For example, 5! (5 factorial) is 5 x 4 x 3 x 2 x 1 = 120.
2. Program Steps
1. Start with the number to find the factorial of.
2. Initialize the result as 1.
3. Use a while loop to multiply the result by the number and decrement the number by 1 each iteration.
4. Continue the loop until the number is reduced to 1.
5. Print the final result, which is the factorial of the starting number.
3. Code Program
# Function to calculate the factorial of a number using a while loop
def factorial_with_while(number):
# Start with the result equal to 1
result = 1
# Loop until the number is reduced to 1
while number > 1:
# Multiply the result by the current number
result *= number
# Decrement the number by 1
number -= 1
# Return the factorial
return result
# Number to calculate the factorial of
num = 5
# Call the function and print the result
factorial_result = factorial_with_while(num)
print(f"The factorial of {num} is: {factorial_result}")
Output:
The factorial of 5 is: 120
Explanation:
1. factorial_with_while is defined to calculate the factorial of number.
2. result is initialized to 1 and will store the intermediate results as the calculation proceeds.
3. A while loop continues to execute as long as number is greater than 1.
4. Within the loop, result is updated to result * number, which accumulates the product of numbers counting down from the starting number.
5. After each multiplication, number is decremented by 1 to proceed to the next lower integer.
6. When number reaches 1, the loop exits, and result now contains the factorial of the original number.
7. The function is used to find the factorial of 5, and the print function displays the result as 120.
8. The output correctly identifies the factorial of 5 as 120.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment