Saturday, March 9, 2024

Program 8 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University - Write a shell script to find the GCD of two given numbers.

 

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

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...