🎓 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
Introduction
The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1. The sequence goes: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, .... Fibonacci numbers have many applications in mathematics, computer science, and nature. This program helps print the Fibonacci series up to a specified number of terms.
Problem Statement
Create a JavaScript program that:
- Accepts a number
n(the number of terms in the Fibonacci sequence). - Prints the first
nterms of the Fibonacci series.
Example:
Input:
7Output:
0, 1, 1, 2, 3, 5, 8Input:
10Output:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Solution Steps
- Read the Input: Provide the number
n(number of terms) either by user input or directly in the code. - Initialize the First Two Terms: Set the first two terms of the Fibonacci series (
0and1). - Calculate the Fibonacci Sequence: Use a loop to calculate the remaining terms by summing the two previous terms.
- Display the Result: Print the Fibonacci series.
JavaScript Program
// JavaScript Program to Print the Fibonacci Series
// Author: https://www.javaguides.net/
function printFibonacciSeries(n) {
let n1 = 0, n2 = 1, nextTerm;
console.log('Fibonacci Series:');
for (let i = 1; i <= n; i++) {
console.log(n1);
nextTerm = n1 + n2;
n1 = n2;
n2 = nextTerm;
}
}
// Example input
let numberOfTerms = 7;
printFibonacciSeries(numberOfTerms);
Output
Fibonacci Series:
0
1
1
2
3
5
8
Example with Different Input
let numberOfTerms = 10;
printFibonacciSeries(numberOfTerms);
Output:
Fibonacci Series:
0
1
1
2
3
5
8
13
21
34
Explanation
Step 1: Initialize the First Two Terms
- The first two terms of the Fibonacci series are initialized as
n1 = 0andn2 = 1.
Step 2: Calculate the Fibonacci Sequence
- A
forloop is used to iterate through the series. The next term is calculated as the sum of the two preceding terms (nextTerm = n1 + n2). - The values of
n1andn2are updated after each iteration to continue generating the next terms.
Step 3: Display the Result
- The terms of the Fibonacci series are printed to the console using
console.log().
Conclusion
This JavaScript program demonstrates how to generate and print the Fibonacci series up to a specified number of terms. By using a simple loop and updating the terms dynamically, the program efficiently computes the Fibonacci sequence. This solution can be extended to handle more advanced Fibonacci-related problems or larger numbers of terms.
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