B.Sc Computer Science with Data analytics
Linux and Shell Programming - Lab
Bharathiar University
Program 6- Write a Shell script to determine whether a given number is a prime number or not.
Source Code
# Function to check if a number is prime
is_prime() {
if [ $1 -le 1 ]; then
echo "$1 is not a prime number."
exit 0
fi
d=2
while [ $d -lt $1 ]; do
if [ $(($1 % $d)) -eq 0 ]; then
echo "$1 is not a prime number."
exit 0
fi
d=$((d + 1))
done
echo "$1 is a prime number."
}
# Main program starts here
# Prompt the user to enter the number
echo "Enter the number: "
read number
# Check if the number is prime
is_prime $number
Sample output:
No comments:
Post a Comment