0% found this document useful (0 votes)
2K views3 pages

Fibonacci Series in Bash

This document contains Bash scripts to calculate the Fibonacci series up to the 6th number, check if a number is prime, and check if a number is a palindrome. The Fibonacci script initializes the first two numbers of the series to 0 and 1, then uses a for loop to calculate each subsequent number by adding the previous two and printing the results. The prime number script takes a number as input, uses a while loop to check if it is divisible by any numbers from 2 to half its value, and prints if it is or isn't prime. The palindrome script reverses the input number, compares it to the original, and prints whether it is the same number backwards and forwards or not.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views3 pages

Fibonacci Series in Bash

This document contains Bash scripts to calculate the Fibonacci series up to the 6th number, check if a number is prime, and check if a number is a palindrome. The Fibonacci script initializes the first two numbers of the series to 0 and 1, then uses a for loop to calculate each subsequent number by adding the previous two and printing the results. The prime number script takes a number as input, uses a while loop to check if it is divisible by any numbers from 2 to half its value, and prints if it is or isn't prime. The palindrome script reverses the input number, compares it to the original, and prints whether it is the same number backwards and forwards or not.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Fibonacci Series in Bash

The Fibonacci numbers are the numbers in the following integer sequence .
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
Approach As we know F0 = 0 and F1 = 1 and the next value comes by adding
the previous two values .
FN = FN-1 + FN-2
Loop to Nth number adding previous two numbers. 

# Program for Fibonacci


# Series

# Static input for N


N=6

# First Number of the


# Fibonacci Series
a=0

# Second Number of the


# Fibonacci Series
b=1

echo "The Fibonacci series is : "

for (( i=0; i<N; i++ ))


do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done
# End of for loop

Bash program to check if the Number is a Prime or not


Prime Numbers: A prime number  is a whole number greater than 1, which is
only divisible by 1 and itself. First few prime numbers are : 2 3 5 7 11 13 17 19
23

#storing the number to be checked


number=43
i=2
#flag variable
f=0

#running a loop from 2 to number/2


while test $i -le `expr $number / 2`
do

#checking if i is factor of number


if test `expr $number % $i` -eq 0
then
f=1
fi

#increment the loop variable


i=`expr $i + 1`
done
if test $f -eq 1
then
echo "Not Prime"
else
echo "Prime"
fi

Bash program to check if the Number is a Palindrome


Given a number num, find whether the given number is palindrome or not using
Bash Scripting. Examples:
Input :
666
Output :
Number is palindrome

Input :
45667
Output :
Number is NOT palindrome

num=545

# Storing the remainder


s=0

# Store number in reverse


# order
rev=""

# Store original number


# in another variable
temp=$num

while [ $num -gt 0 ]


do
# Get Remainder
s=$(( $num % 10 ))

# Get next digit


num=$(( $num / 10 ))

# Store previous number and


# current digit in reverse
rev=$( echo ${rev}${s} )
done

if [ $temp -eq $rev ];


then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi

You might also like