B.Sc Computer Science with Data analytics
Linux and Shell Programming - Lab
Bharathiar University
Program 8- Write a shell script to find the GCD of two given numbers.
Source Code
# Function to find the GCD using the Euclidean algorithm
gcd() {
dividend=$1
divisor=$2
while [ $divisor -ne 0 ]; do
remainder=$((dividend % divisor))
dividend=$divisor
divisor=$remainder
done
echo "$dividend"
}
# Main program starts here
# Prompt the user to enter the first number
echo "Enter the first number: "
read num1
# Prompt the user to enter the second number
echo "Enter the second number: "
read num2
# Find the GCD of the two numbers
result=$(gcd $num1 $num2)
echo "The GCD of $num1 and $num2 is: $result"
Sample Output:
No comments:
Post a Comment