Lyo001 / Class

0 stars 0 forks source link

Work #1

Open Lyo001 opened 2 weeks ago

Lyo001 commented 2 weeks ago

!/bin/bash

factorial() { n=$1 fact=1 while [ $n -gt 1 ]; do fact=$((fact * n)) n=$((n - 1)) done echo $fact }

calculate_sum() { n=$1 sum=0

for (( i=1; i<=n; i++ )); do fact=$(factorial $i) term=$(echo "scale=10; 1/$fact" | bc) sum=$(echo "scale=10; $sum + $term" | bc) done

echo "Sum of the series up to 1/$n! is: $sum" }

echo -n "Enter the value of n: " read n

calculate_sum $n

Lyo001 commented 2 weeks ago

!/bin/bash

echo -n "Enter the value of n: " read n

sum=0 fact=1

for (( i=1; i<=n; i++ )); do fact=$((fact * i))

sum=$(echo "scale=10; $sum + 1/$fact" | bc) done

echo "Sum of the series up to 1/$n! is: $sum"

Lyo001 commented 2 weeks ago

echo "Enter an integer:" read num reversed=" " while [ $num -ne 0 ] do digit=$((num % 10)) reversed="${reversed}${digit}" num=$((num / 10)) done echo "Reversed digits: $reversed"

Lyo001 commented 2 weeks ago

!/bin/bash

Get employee's yearly salary

read -p "Enter the employee's yearly salary: " yearly_salary

Check if the salary is greater than or equal to 5000

if [[ $yearly_salary -ge 5000 ]]; then

Calculate 5% bonus

bonus=$(echo "scale=2; $yearly_salary * 0.05" | bc) echo "Bonus: ₹$bonus" else

Fixed bonus of ₹250

bonus=250 echo "Bonus: ₹$bonus" fi

Lyo001 commented 2 weeks ago

!/bin/bash

echo "Enter a list of numbers:" read -a num n=${#num[@]} for ((i=0; i<n; i++)) do for ((j=0; j<n-i-1; j++)) do if [ ${num[j]} -lt ${num[$((j+1))]}] then temp=${num[j]} num[j]=${num[$((j+1))]]} num[$((j+1))]=$temp fi done done echo "Ascending order: ${num[@]}"

for ((i=0; i<n; i++)) do for ((j=0; j<n-i-1; j++)) do if [ ${num[j]} -gt ${num[$((j+1))]}] then temp=${num[j]} num[j]=${num[$((j+1))]]} num[$((j+1))]=$temp fi done done echo "Descending order: ${num[@]}"

Lyo001 commented 2 weeks ago

"Enter the employee's annual salary:" read salary if [ "$salary" -ge 5000 ]] then bonus=$(echo "$salary * 0.05" | bc) else bonus=250 fi echo "The bonus for the employee is: Rs $bonus"