0% found this document useful (0 votes)
3 views5 pages

Os Lab

The document contains several shell scripts for basic mathematical operations. It includes programs to add two numbers, calculate the factorial of a number, find twin prime numbers in a range, sum the Fibonacci series from 1 to 10, and compute simple interest based on user inputs. Each script is accompanied by example outputs demonstrating their functionality.

Uploaded by

rakhiray11223344
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)
3 views5 pages

Os Lab

The document contains several shell scripts for basic mathematical operations. It includes programs to add two numbers, calculate the factorial of a number, find twin prime numbers in a range, sum the Fibonacci series from 1 to 10, and compute simple interest based on user inputs. Each script is accompanied by example outputs demonstrating their functionality.

Uploaded by

rakhiray11223344
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
You are on page 1/ 5

1) Write a shell program to add two numbers.

 #!/bin/bash

# Prompt the user for the first number


echo "Enter the first number:"
read num1

# Prompt the user for the second number


echo "Enter the second number:"
read num2

# Calculate the sum


sum=$((num1 + num2))

# Display the result


echo "The sum of $num1 and $num2 is: $sum"

OUTPUT
Enter the first number:
5
Enter the second number:
10
The sum of 5 and 10 is: 15

2. Write a shell program to find factorial of a given number.


 #!/bin/bash

# Function to calculate factorial


factorial() {
local num=$1
local fact=1

# Loop to calculate factorial


for (( i=1; i<=num; i++ ))
do
fact=$((fact * i))
done
echo $fact
}

# Prompt the user for a number


echo "Enter a number to find its factorial:"
read number

# Check if the input is a non-negative integer


if [[ $number -lt 0 ]]; then
echo "Factorial is not defined for negative numbers."
elif [[ $number -eq 0 ]]; then
echo "The factorial of 0 is 1."
else
result=$(factorial $number)
echo "The factorial of $number is: $result"
fi

OUTPUT

Enter a number to find its factorial:


5
The factorial of 5 is: 120

3. Write a shell program to fine twin prime numbers between a given range.
 #!/bin/bash

# Function to check if a number is prime


is_prime() {
local num=$1
if [[ $num -lt 2 ]]; then
return 1 # Not prime
fi
for (( i=2; i*i<=num; i++ ))
do
if [[ $((num % i)) -eq 0 ]]; then
return 1 # Not prime
fi
done
return 0 # Prime
}

# Prompt the user for the range


echo "Enter the starting number of the range:"
read start
echo "Enter the ending number of the range:"
read end

# Check if the range is valid


if [[ $start -ge $end ]]; then
echo "Invalid range! The starting number must be less than the ending
number."
exit 1
fi

echo "Twin prime numbers between $start and $end are:"

# Loop through the range and find twin primes


for (( num=start; num<=end-2; num++ ))
do
if is_prime $num && is_prime $((num + 2)); then
echo "($num, $((num + 2)))"
fi
done

OUTPUT
Enter the starting number of the range:
10
Enter the ending number of the range:
50
Twin prime numbers between 10 and 50 are:
(11, 13)
(17, 19)
(29, 31)
(41, 43)

4. Write a shell program to find the sum of Fibonacci series from 1 to 10


 #!/bin/bash

# Initialize variables
a=0
b=1
sum=0

echo "Fibonacci series from 1 to 10:"

# Loop to generate Fibonacci numbers and calculate their sum


for (( i=1; i<=10; i++ ))
do
echo -n "$b " # Print the current Fibonacci number
sum=$((sum + b)) # Add the current Fibonacci number to the sum
next=$((a + b)) # Calculate the next Fibonacci number
a=$b # Update the previous number
b=$next # Update the current number
done

echo # Print a newline


echo "The sum of the Fibonacci series from 1 to 10 is: $sum"

OUTPUT
Fibonacci series from 1 to 10:
1 1 2 3 5 8 13 21 34 55
The sum of the Fibonacci series from 1 to 10 is: 143
5. Write a shell program to find the simple interest(si) , where principal(p),
time(t) and rate of interest(r) are user choice.
[Using this formula, si=(p*t*r)/100]

 #!/bin/bash

# Prompt the user for input


echo "Enter the principal amount (P):"
read p

echo "Enter the time period in years (T):"


read t

echo "Enter the rate of interest (R):"


read r

# Calculate the simple interest


si=$(( (p * t * r) / 100 ))

# Display the result


echo "The Simple Interest (SI) is: $si"

OUTPUT
Enter the principal amount (P):
1000
Enter the time period in years (T):
5
Enter the rate of interest (R):
10
The Simple Interest (SI) is: 500

You might also like