0% found this document useful (0 votes)
3 views

Array Bash

Uploaded by

Hasnat Zamil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Array Bash

Uploaded by

Hasnat Zamil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Sum of even and odd number


# Function to calculate sum of odd numbers

sum_odd() {

local sum=0

for num in "$@"; do

if [ $((num % 2)) -ne 0 ]; then

sum=$((sum + num))

fi

done

echo $sum

# Function to calculate sum of even numbers

sum_even() {

local sum=0

for num in "$@"; do

if [ $((num % 2)) -eq 0 ]; then

sum=$((sum + num))

fi

done

echo $sum

# Main program starts here

echo "Enter numbers separated by spaces:"

read -a numbers

# Calculate sums

sum_odd=$(sum_odd "${numbers[@]}")

sum_even=$(sum_even "${numbers[@]}")
# Display results

echo "Sum of odd numbers: $sum_odd"

echo "Sum of even numbers: $sum_even"

2. Write a Shell program to find the average of n numbers.


# Function to calculate the average of numbers

calculate_average() {

local total=0

local count=0

for num in "$@"; do

total=$((total + num))

count=$((count + 1))

done

if [ $count -gt 0 ]; then

average=$((total / count))

echo $average

else

echo "No numbers provided to calculate average."

fi

# Main program starts here

echo "Enter numbers separated by spaces:"

read -a numbers

# Calculate average

average=$(calculate_average "${numbers[@]}")

# Display result
echo "Average of the provided numbers: $average"

3. Write a Shell Program to find the largest element of an array.

# Function to find the largest element in an array


find_largest() {
local max=$1
for num in "${@:2}"; do
if [ $num -gt $max ]; then
max=$num
fi
done
echo $max
}

# Main program starts here


echo "Enter numbers separated by spaces:"
read -a numbers

# Find the largest element


largest=$(find_largest "${numbers[@]}")

# Display the result


echo "The largest element in the array is: $largest"

4 Function to find the smallest element in an array


find_smallest() {
local min=$1
for num in "${@:2}"; do
if [ $num -lt $min ]; then
min=$num
fi
done
echo $min
}

# Main program starts here


echo "Enter numbers separated by spaces:"
read -a numbers

# Find the smallest element


smallest=$(find_smallest "${numbers[@]}")

# Display the result


echo "The smallest element in the array is: $smallest"
5 Function to find the largest number between two numbers
find_largest() {
if [ $1 -gt $2 ]; then
echo $1
else
echo $2
fi
}

# Main program starts here


echo "Enter the first number:"
read num1

echo "Enter the second number:"


read num2

# Call the function to find the largest number


largest=$(find_largest $num1 $num2)

# Display the result


echo "The largest number between $num1 and $num2 is: $largest"

# Function to calculate the sum of odd digits


sum_odd_digits() {
local num=$1
local sum=0

while [ $num -gt 0 ]; do


digit=$((num % 10))
if [ $((digit % 2)) -ne 0 ]; then
sum=$((sum + digit))
fi
num=$((num / 10))
done

echo $sum
}

6. Function to calculate the sum of even digits


sum_even_digits() {
local num=$1
local sum=0

while [ $num -gt 0 ]; do


digit=$((num % 10))
if [ $((digit % 2)) -eq 0 ]; then
sum=$((sum + digit))
fi
num=$((num / 10))
done

echo $sum
}

# Main program starts here


echo "Enter a number:"
read number

# Calculate the sum of odd digits


odd_sum=$(sum_odd_digits $number)

# Calculate the sum of even digits


even_sum=$(sum_even_digits $number)

# Display results
echo "Sum of odd digits: $odd_sum"
echo "Sum of even digits: $even_sum"

7. Function to find the largest digit in a number


find_largest_digit() {
local num=$1
local largest=0

while [ $num -gt 0 ]; do


digit=$((num % 10))
if [ $digit -gt $largest ]; then
largest=$digit
fi
num=$((num / 10))
done

echo $largest
}

# Main program starts here


echo "Enter a number:"
read number

# Call the function to find the largest digit


largest=$(find_largest_digit $number)

# Display the result


echo "The largest digit in the number is: $largest"

You might also like