0% found this document useful (0 votes)
22 views4 pages

Saksham OS Practical File

Os practical file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Saksham OS Practical File

Os practical file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab Exercise – 4 (i)

Aim: Write a script to print Fibonacci series up to n terms

Software Used: Linux

Theory:

A script to print the Fibonacci series up to n terms generates a sequence of numbers where each number is the
sum of the two preceding ones, starting from 0 and 1. The script works by first accepting the total number of
terms n from the user. It then initializes the first two numbers of the series and uses a loop to calculate and display
the subsequent terms by repeatedly adding the last two numbers. This script demonstrates essential programming
concepts such as loops, variable manipulation, and sequence generation, and is commonly used in mathematical
computations and algorithm practice.

Source Code:
#!/bin/bash

read -p "Enter the number of terms: " n

a=0

b=1

echo "Fibonacci series up to $n terms:"

for (( i=1; i<=n; i++ ))

do
echo -n "$a "

fn=$((a + b))

a=$b

b=$fn

done

echo

Output:
Lab Exercise – 4 (ii)
Aim- Write a script to calculate the factorial of a given number

Software Used: Linux

Theory:

script to calculate the factorial of a given number computes the product of all positive integers up to that number.
The factorial of a number n, denoted as n!, is calculated as n × (n-1) × (n-2) × ... × 1. The script works by taking
the input number from the user and using either a loop or recursion to multiply the sequence of integers to obtain
the factorial. This type of script demonstrates key programming concepts such as loops, variable manipulation,
arithmetic operations, and input handling, and is widely used in mathematical computations, probability, and
combinatorial problems.

Source Code:
#!/bin/bash

read -p "Enter a number: " n

factorial=1

for (( i=1; i<=n; i++ ))

do

factorial=$((factorial * i))

done
echo "Factorial of $n is: $factorial"

Output:
Lab Exercise – 4 (iii)
Aim: Write a script to calculate the sum of digits of a given number

Software Used: Linux

Theory:

A script to calculate the sum of digits of a given number computes the total of all individual digits in that number.
The script works by taking the number as input from the user and then extracting each digit using arithmetic
operations, typically the modulo and division operations. Each extracted digit is added to a running total until all
digits are processed. This script demonstrates important programming concepts such as loops, arithmetic
operations, and variable manipulation, and is commonly used in numerical analysis, data validation, and digital
computation tasks.

Source Code:
#!/bin/bash

read -p "Enter a number: " num

sum=0

n=$num

while [ $n -gt 0 ]

do

digit=$(( n % 10 ))
sum=$(( sum + digit ))

n=$(( n / 10 ))

done

echo "Sum of digits of $num is: $sum"

Output:
Lab Exercise – 4 (iv)
Aim: Write a script to check whether the given string is palindrome or not

Software Used: Linux

Theory:

A script to check whether a given string is a palindrome determines if the string reads the same forwards and
backwards. The script works by comparing characters from the beginning and end of the string moving towards
the center, or by reversing the string and checking if it matches the original. Palindromes ignore character order
and symmetry, making them important in text processing, data validation, and certain algorithmic problems. This
type of script demonstrates key programming concepts such as string manipulation, loops, conditional statements,
and logical comparison.

Source Code:
#!/bin/bash

read -p "Enter a string: " str

rev=$(echo "$str" | rev)

if [ "$str" == "$rev" ]; then

echo "\"$str\" is a palindrome."

else

echo "\"$str\" is not a palindrome."


fi

Output:

You might also like