Write the code to find the Fibonacci series upto the nth term.
Introduction:
Fibonacci series is one of the classic programming challenges. This problem not only tests a coder's fundamental understanding but also their ability to think algorithmically across different programming languages.
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1.
1. Java
import java.util.Scanner;
public class FibonacciSeries {
// Function to print Fibonacci series up to n terms
public static void printFibonacci(int n) {
int a = 0, b = 1, next;
System.out.print("Fibonacci Series: ");
// Loop to generate Fibonacci sequence up to n terms
for (int i = 1; i <= n; i++) {
System.out.print(a + " "); // Print the current term
// Calculate the next term
next = a + b;
// Update a and b for the next iteration
a = b;
b = next;
}
System.out.println(); // Print a newline at the end
}
// Main Method
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = scanner.nextInt(); // Input number of terms
printFibonacci(n); // Call the function to print the series
scanner.close();
}
}
#include <iostream>
using namespace std;
// Function to print Fibonacci series up to n terms
void printFibonacci(int n) {
int a = 0, b = 1, next;
cout << "Fibonacci Series: ";
// Loop to generate Fibonacci sequence up to n terms
for (int i = 1; i <= n; i++) {
cout << a << " "; // Print the current term
// Calculate the next term
next = a + b;
// Update a and b for the next iteration
a = b;
b = next;
}
cout << endl; // Print a newline at the end
}
int main() {
int n;
cout << "Enter the number of terms: ";
cin >> n; // Input number of terms
printFibonacci(n); // Call the function to print the series
return 0;
}
#include <stdio.h>
// Function to print Fibonacci series up to n terms
void printFibonacci(int n) {
int a = 0, b = 1, next;
printf("Fibonacci Series: ");
// Loop to generate Fibonacci sequence up to n terms
for (int i = 1; i <= n; i++) {
printf("%d ", a); // Print the current term
// Calculate the next term
next = a + b;
// Update a and b for the next iteration
a = b;
b = next;
}
printf("\n"); // Print a newline at the end
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n); // Input number of terms
printFibonacci(n); // Call the function to print the series
return 0;
}
Sample Input & Output:
Input: 5
Fibonacci Series: 0 1 1 2 3
Input: 8
Fibonacci Series: 0 1 1 2 3 5 8 13
Explanation of Code:
- Each code version defines a
printFibonacci
function to generate and display the Fibonacci series up to then
th term. - Loop: It iterates
n
times to print each term. - Variable Updates: The
a
andb
variables represent consecutive terms in the series, and we use a temporary variablenext
(or a tuple assignment in Python) to keep adding the previous two terms.
This code will output the Fibonacci sequence starting from 0, up to the number of terms specified by the user.
** Please do subscribe my blog for future updates and share with your friends, if you find this informative **
Comments
Post a Comment