Knowee
Questions
Features
Study Tools

Write a shell script to print the n terms in a Fibonacci Series

Question

Write a shell script to print the n terms in a Fibonacci Series

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple shell script to print the n terms in a Fibonacci series:

#!/bin/bash

# Read the number of terms
echo "Enter the number of terms: "
read n

# First two terms
a=0
b=1

# Print first two terms
echo "Fibonacci Series: "
echo -n "$a $b "

# Find remaining terms
for (( i=3; i<=n; i++ ))
do
    c=$((a + b))
    echo -n "$c "
    a=$b
    b=$c
done

# Print a new line
echo

Here's how this script works:

  1. It first reads the number of terms to be printed from the user.
  2. It initializes the first two terms of the Fibonacci series, 0 and 1.
  3. It then prints these two terms.
  4. It enters a loop that runs from the third term up to the nth term.
  5. In each iteration of the loop, it calculates the next term as the sum of the previous two terms, prints it, and then updates the previous two terms.
  6. Finally, it prints a new line for formatting purposes.

This problem has been solved

Similar Questions

create a C program to print Fibonacci series upto a specified number N using while loop.

Write a C program to print the Fibonacci series using recursion and without using recursion.

WAP a program to generate a list of elements of Fibonacci Series.

Create a program that generates and prints the Fibonacci series up to a specified number 'N'. The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding numbers, starting with 0 and 1. Your program should take an integer input 'N' and display the Fibonacci series up to the Nth term using a while loop.Fibonacci series: 0, 1, 1, 2, 3, 5, 8,... Note: This question is one of the most asked questions in placements.Input format :The input consists of a positive integer N.Output format :The output displays the Fibonacci series up to the Nth term separated by space.

create a C program that generates Fibonacci series upto a specified number N.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.