1. Check whether the given number is prime or not.
Aim: To determine if a user-provided integer is a prime number.
Procedure:
o Prompt the user to enter a number.
o Read the input number.
o Implement a loop to check for divisibility by numbers from 2
up to the square root of the input number.
o If a divisor is found, declare it as not prime; otherwise, declare
it as prime.
#!/bin/bash
echo "Enter a number:"
read num
i=2
is_prime=1
if [ "$num" -lt 2 ]; then
is_prime=0
else
while [ "$i" -le "$((num / 2))" ]; do
if [ "$((num % i))" -eq 0 ]; then
is_prime=0
break
fi
i=$((i + 1))
done
fi
if [ "$is_prime" -eq 1 ]; then
echo "$num is a prime number."
else
echo "$num is not a prime number."
Fi
2. Find the biggest of given two numbers.
Aim: To identify the larger of two user-provided integers.
Procedure:
o Prompt the user to enter two numbers.
o Read both numbers.
o Use an if-else statement to compare the two numbers and print
the larger one.
#!/bin/bash
echo "Enter first number:"
read num1
echo "Enter second number:"
read num2
if [ "$num1" -gt "$num2" ]; then
echo "$num1 is the biggest."
else
echo "$num2 is the biggest."
Fi
3. To check whether the given number is odd or even.
Aim: To determine if a user-provided integer is odd or even.
Procedure:
o Prompt the user to enter a number.
o Read the input number.
o Use the modulo operator (%) to check if the number is divisible
by 2.
o If the remainder is 0, it's even; otherwise, it's odd.
#!/bin/bash
echo "Enter a number:"
read num
if [ "$((num % 2))" -eq 0 ]; then
echo "$num is an even number."
else
echo "$num is an odd number."
Fi
4. To generate Fibonacci Series.
Aim:
To generate and display a specified number of terms in the Fibonacci
sequence.
Procedure:
Prompt the user for the number of terms to generate.
Initialize the first two Fibonacci numbers (0 and 1).
Use a loop to calculate subsequent terms by summing the
previous two, printing each term.
#!/bin/bash
echo "Enter the number of terms for Fibonacci series:"
read n
a=0
b=1
echo "Fibonacci Series:"
for (( i=0; i<n; i++ )); do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done
echo
5. To prepare an electric bill for domestic consumers.
Aim:
To calculate and display an electricity bill based on units consumed and a
tiered pricing structure.
Procedure:
Get customer details: Customer No., Customer Name,
Pre. Reading, Cur. Reading.
Calculate Units Consumed (Cur. Reading - Pre. Reading).
Implement conditional logic to calculate the charge based on
the provided unit rates.
Display the bill in the specified format.
#!/bin/bash
echo "Customer No.:"
read cust_no
echo "Customer Name:"
read cust_name
echo "Previous Reading:"
read prev_reading
echo "Current Reading:"
read curr_reading
units_consumed=$((curr_reading - prev_reading))
charge=0
if [ "$units_consumed" -le 100 ]; then
charge=$(echo "$units_consumed * 0.75" | bc)
elif [ "$units_consumed" -le 200 ]; then
charge=$(echo "100 * 0.75 + ($units_consumed - 100) * 1.50" | bc)
else
charge=$(echo "100 * 0.75 + 100 * 1.50 + ($units_consumed - 200) *
3.00" | bc)
fi
echo "Customer No. -------- $cust_no"
echo "Customer Name ----------- $cust_name"
echo "Pre. Reading ----------- $prev_reading"
echo "Cur. Reading----------- $curr_reading"
echo "Units Consumed ----------- $units_consumed"
echo "Charge ----------- Rs. $charge"
echo "Signature ---------------------"
6. Write a program to display the result PASS or FAIL.
Aim:
To determine a student's overall pass/fail status based on individual
subject marks.
Procedure:
Get Student Name, Student Reg. No., and Mark1, Mark2, Mark3,
Mark4.
Check if each mark is greater than or equal to 50.
If all marks meet the minimum, declare "PASS"; otherwise,
declare "FAIL".
#!/bin/bash
echo "Student Name:"
read s_name
echo "Student Reg. No.:"
read reg_no
echo "Mark 1:"
read m1
echo "Mark 2:"
read m2
echo "Mark 3:"
read m3
echo "Mark 4:"
read m4
if [ "$m1" -ge 50 ] && [ "$m2" -ge 50 ] && [ "$m3" -ge 50 ] && [ "$m4" -ge
50 ]; then
result="PASS"
else
result="FAIL"
fi
echo "Student Name: $s_name"
echo "Student Reg. No.: $reg_no"
echo "Result: $result"
7. Write a program to prepare a Payroll.
Aim: To calculate and display various components of an employee's
payroll.
Procedure:
o Get Basic Pay.
o Calculate DA, Allowances, PF based on predefined percentages
or fixed values.
o Calculate Gross Pay (Basic Pay + DA + Allowances).
o Calculate Net Pay (Gross Pay - PF).
o Display all payroll components.
#!/bin/bash
echo "Enter Basic Pay:"
read basic_pay
da=$(echo "$basic_pay * 0.10" | bc) # Example DA (10% of Basic)
allowances=$(echo "$basic_pay * 0.05" | bc) # Example Allowances (5%
of Basic)
pf=$(echo "$basic_pay * 0.12" | bc) # Example PF (12% of Basic)
gross_pay=$(echo "$basic_pay + $da + $allowances" | bc)
net_pay=$(echo "$gross_pay - $pf" | bc)
echo "Basic Pay: $basic_pay"
echo "DA: $da"
echo "Allowances: $allowances"
echo "PF: $pf"
echo "Gross Pay: $gross_pay"
echo "Net Pay: $net_pay"
8. Using Case Statement, write a program to check the files ending with
vowels.
Aim: To identify files in the current directory whose names end with
a vowel.
Procedure:
o Loop through files in the current directory.
o Extract the last character of each filename.
o Use a case statement to check if the last character is a vowel (a,
e, i, o, u, A, E, I, O, U).
o Print the filename if it ends with a vowel.
9. Write a program to sort the names in alphabetical order, numbers in
ascending and descending order.
Aim: To sort lists of names and numbers in specified orders.
Procedure:
o Names: Read a list of names and use the sort command
(e.g., sort -f for case-insensitive) to sort alphabetically.
o Numbers (Ascending): Read a list of numbers and use sort -
n to sort numerically in ascending order.
o Numbers (Descending): Read a list of numbers and use sort -
nr to sort numerically in descending order.
#!/bin/bash
echo "Enter filename:"
read filename
last_char="${filename: -1}"
case "$last_char" in
[aeiouAEIOU])
echo "The file '$filename' ends with a vowel."
;;
*)
echo "The file '$filename' does not end with a vowel."
;;
Esac
10. Write a menu driven program to print Bio-data for five persons.
Aim:
To provide a menu-driven interface to display bio-data for pre-defined
individuals.
Procedure:
Store bio-data for five persons (e.g., in variables or a simple
data structure within the script).
Display a menu with options to view bio-data for each person.
Use a case statement or if-elif-else to respond to user input and
display the corresponding bio-data.
Include an option to exit the program.
#!/bin/bash
# Sort names
echo "Enter names (one per line, press Ctrl+D to finish):"
names=$(cat)
echo "Names in alphabetical order:"
echo "$names" | sort
# Sort numbers
echo "Enter numbers (one per line, press Ctrl+D to finish):"
numbers=$(cat)
echo "Numbers in ascending order:"
echo "$numbers" | sort -n
echo "Numbers in descending order:"
echo "$numbers" | sort -nr