Saturday, March 9, 2024

Program 9 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University - Write a shell script to check whether given string is palindrome or not.

 

B.Sc Computer Science  with Data analytics

Linux and Shell Programming - Lab  

Bharathiar University 

Program 9-  Write a shell script to check whether given string is palindrome or not. 


Source Code


# Main program starts here


# Prompt the user to enter the string

    echo "Enter the string: "

    read str

    reversed=$(echo "$str" | rev)

    if [ "$str" = "$reversed" ]; then

        echo "The string '$str' is a palindrome."

    else

        echo "The string '$str' is not a palindrome."

    fi



Sample OUTPUT:



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:



Thursday, March 7, 2024

Program 7 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University - Write a Shell script to print the first n Fibonacci numbers

 

B.Sc Computer Science  with Data analytics

Linux and Shell Programming - Lab  

Bharathiar University 

Program 7- Write a Shell script to print the first n Fibonacci numbers


Source Code


# Function to print the first n Fibonacci numbers
print_fibonacci() {

    # Initialize variables for the first two Fibonacci numbers
    num1=0
    num2=1
    count=0

    # Print the first two Fibonacci numbers
    echo "The first $n Fibonacci numbers are:"
    echo "$num1"
    echo "$num2"

    # Calculate and print the remaining Fibonacci numbers
    while [ $count -lt $(($n - 2)) ]; do
        next=$((num1 + num2))
        echo "$next"
        num1=$num2
        num2=$next
        count=$((count + 1))
    done
}

# Main program starts here
# Prompt the user to enter the number
    echo "Enter the a number of fibonacci series to print: "
    read n

# Check if n is a positive integer
if  [[ "$n" -lt 1 ]]; then
    echo "Error: Please provide a positive integer."
    exit 1
fi

# Print the first n Fibonacci numbers
print_fibonacci $n


Sample Output:


Tuesday, March 5, 2024

Program 6 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University - Write a Shell script to determine whether a given number is a prime number or not.

 


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:



Program 5 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University -Write a shell script (small calculator) that adds, subtracts, multiplies and divides the two given numbers.

 

B.Sc Computer Science  with Data analytics

Linux and Shell Programming - Lab  

Bharathiar University 

Program 5- -Write a shell script (small calculator) that adds, subtracts, multiplies and divides the two given numbers. 

Source Code



# Function to perform addition
addition() {
    echo "$num1 + $num2" | bc
}

# Function to perform subtraction
subtraction() {
    echo "$num1 - $num2" | bc
}

# Function to perform multiplication
multiplication() {
    echo "$num1 * $num2" | bc
}

# Function to perform division
division() {
    if [ "$num2" -eq 0 ]; then
        echo "Error: Division by zero!"
    else
        echo "scale=2; $num1 / $num2" | bc
    fi
}

# Main script 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
       
while true; do
    # Prompt the user to select the operation
    echo "Select the operation:"
    echo "1. Addition"
    echo "2. Subtraction"
    echo "3. Multiplication"
    echo "4. Division"
    echo "5. Exit"
    echo "Enter your choice: "
    read choice

       
    case $choice in
        1) echo "Addition: $(addition)";;
        2) echo "Subtraction: $(subtraction)";;
        3) echo "Multiplication: $(multiplication)";;
        4) echo "Division: $(division)";;
        5) echo "Exiting..."; exit;;
        *) echo "Invalid choice";;
    esac
done


SAMPLE OUTPUT:




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...