Write a program to print the Fibonacci series. content 1 Acclaimed
Table of Contents
Algorithm
- Start
- Read the number of terms, n, from the user.
- Check if n is less than or equal to 0. If true, print an error message and stop.
- Initialize variables: num1 = 0, num2 = 1, count = 0.
- Print “Fibonacci Series:” to indicate the start of the series.
- While count is less than n, repeat steps 7-9.
- Print the value of num1.
- Calculate the next Fibonacci number: num_next = num1 + num2.
- Update the values of num1 and num2: num1 = num2, num2 = num_next. Increment count by 1.
- Stop.
C program
#include <stdio.h> void fibonacciSeries(int n) { // Check if the input is valid if (n <= 0) { printf("Please enter a positive integer.\n"); return; } // First two numbers of the series int num1 = 0, num2 = 1; // Print the Fibonacci series printf("Fibonacci Series:\n"); for (int i = 0; i < n; i++) { printf("%d ", num1); int num_next = num1 + num2; num1 = num2; num2 = num_next; } printf("\n"); } int main() { int n; printf("Enter the number of terms: "); scanf("%d", &n); // Call the function to print the Fibonacci series fibonacciSeries(n); return 0; }
When you run this program, it will ask you to enter the number of terms you want to print in the Fibonacci series. It will then print the Fibonacci series up to that number of terms.
C++
#include <iostream> void fibonacciSeries(int n) { // Check if the input is valid if (n <= 0) { std::cout << "Please enter a positive integer." << std::endl; return; } // First two numbers of the series int num1 = 0, num2 = 1; // Print the Fibonacci series std::cout << "Fibonacci Series:" << std::endl; for (int i = 0; i < n; i++) { std::cout << num1 << " "; int num_next = num1 + num2; num1 = num2; num2 = num_next; } std::cout << std::endl; } int main() { int n; std::cout << "Enter the number of terms: "; std::cin >> n; // Call the function to print the Fibonacci series fibonacciSeries(n); return 0; }
When you run this program, it will ask you to enter the number of terms you want to print in the Fibonacci series. It will then print the Fibonacci series up to that number of terms.
JAVA
import java.util.Scanner; public class FibonacciSeries { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of terms: "); int n = scanner.nextInt(); // Call the function to print the Fibonacci series fibonacciSeries(n); scanner.close(); } public static void fibonacciSeries(int n) { // Check if the input is valid if (n <= 0) { System.out.println("Please enter a positive integer."); return; } // First two numbers of the series int num1 = 0, num2 = 1; // Print the Fibonacci series System.out.println("Fibonacci Series:"); for (int i = 0; i < n; i++) { System.out.print(num1 + " "); int num_next = num1 + num2; num1 = num2; num2 = num_next; } System.out.println(); } }
When you run this program, it will ask you to enter the number of terms you want to print in the Fibonacci series. It will then print the Fibonacci series up to that number of terms.
python
def fibonacci_series(n): # Check if the input is valid if n <= 0: print("Please enter a positive integer.") return # First two numbers of the series num1, num2 = 0, 1 # Print the Fibonacci series print("Fibonacci Series:") for _ in range(n): print(num1, end=" ") num_next = num1 + num2 num1 = num2 num2 = num_next print() # Get the input from the user n = int(input("Enter the number of terms: ")) # Call the function to print the Fibonacci series fibonacci_series(n)
When you run this program, it will ask you to enter the number of terms you want to print in the Fibonacci series. It will then print the Fibonacci series up to that number of terms.
his algorithm, we start with the first two terms of the Fibonacci series, which are 0 and 1. We then iterate n-2 times to calculate and print the subsequent terms of the series. The variables num1 and num2 are used to keep track of the current and next numbers in the series. After printing each term, we update the variables accordingly.