🎓 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
The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding numbers. The simplest sequence starts with 0 and 1. In this blog post, we'll develop a Python program that displays the Fibonacci series up to a given number of terms.
2. Program Overview
Our program will take an integer input from the user to determine the number of terms to display. We will then calculate and display the Fibonacci series up to the desired number of terms using iteration.
3. Code Program
def fibonacci(n):
"""Return the Fibonacci series up to n terms."""
series = []
a, b = 0, 1
for _ in range(n):
series.append(a)
a, b = b, a + b
return series
# Input number of terms from user
num_terms = int(input("How many terms? "))
# Ensure the number is non-negative
if num_terms <= 0:
print("Please enter a positive integer.")
else:
print("Fibonacci series:")
print(", ".join(map(str, fibonacci(num_terms))))
Output:
How many terms? 10 Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
4. Step By Step Explanation
1. The fibonacci function calculates the Fibonacci series up to n terms using iteration.
2. We initialize two variables, a and b, with values 0 and 1, which are the first two terms of the series.
3. In each iteration of the for loop, we append the value of a to our series list.
4. The new values of a and b are then calculated by swapping the values (using tuple unpacking) and adding them together.
5. In the main section, the program prompts the user for input. If the number of terms is less than or equal to 0, it informs the user to enter a positive integer.
6. Otherwise, it calls the fibonacci function and displays the result.
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