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

1st part lab manual 4

Uploaded by

Hasnat Zamil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

1st part lab manual 4

Uploaded by

Hasnat Zamil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Function to generate Fibonacci series


generate_fibonacci() {

local n=$1

local a=0

local b=1

echo "Fibonacci series up to $n terms:"

for ((i = 0; i < n; i++)); do

echo -n "$a "

fn=$((a + b))

a=$b

b=$fn

done

echo

echo "Enter the number of terms for Fibonacci series:"

read num_terms

generate_fibonacci $num_terms

2. Write a Shell program to find the factorial of a number using for loop.
calculate_factorial() {

local num=$1

local factorial=1

# Loop to calculate factorial

for ((i = 1; i <= num; i++)); do

factorial=$((factorial * i))

done

echo $factorial

}
3. Write a Shell program to find the smallest number from a set of numbers.
find_smallest() {

local smallest=$1

for num in "${@:2}"; do

if [ $num -lt $smallest ]; then

smallest=$num

fi

done

echo $smallest

# Main program starts here

echo "Enter numbers separated by spaces:"

read -a numbers

if [ ${#numbers[@]} -eq 0 ]; then

echo "No numbers provided."

exit 1

fi

# Find the smallest number

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

echo "The smallest number is: $smallest"

echo "Enter a number:"

read number

# Check if the number is negative


if [ $number -lt 0 ]; then

echo "Factorial is not defined for negative numbers."

exit 1

fi

# Call the function to calculate factorial

result=$(calculate_factorial $number)

# Display the result

echo "Factorial of $number is: $result"

# Function to find the 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

4. Write a Shell program to find the sum of odd and even numbers from a set
of 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

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

even_sum=$(sum_even "${numbers[@]}")

# Display results

echo "Sum of odd numbers: $odd_sum"

echo "Sum of even numbers: $even_sum"

5. Write a Shell program to find the smallest digit from a number.


find_smallest_digit() {

local num=$1

local smallest=9

while [ $num -gt 0 ]; do

digit=$((num % 10))

if [ $digit -lt $smallest ]; then

smallest=$digit

fi

num=$((num / 10))

done

echo $smallest

}
# Main program starts here

echo "Enter a number:"

read number

# Call the function to find the smallest digit

smallest=$(find_smallest_digit $number)

# Display the result

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

6 Write a Shell program to check the given integer is Armstrong number or


not.
is_armstrong() {

local number=$1

local original_number=$number

local num_digits=${#number}

local sum=0

while [ $number -gt 0 ]; do

digit=$((number % 10))

sum=$((sum + digit ** num_digits))

number=$((number / 10))

done

if [ $sum -eq $original_number ]; then

echo "$original_number is an Armstrong number."

else

echo "$original_number is not an Armstrong number."

fi
}

# Main program starts here

echo "Enter an integer:"

read number

7 Write a Shell program to find the sum of digits of a number.


is_armstrong $number

sum_of_digits() {

local num=$1

local sum=0

while [ $num -gt 0 ]; do

digit=$((num % 10))

sum=$((sum + digit))

num=$((num / 10))

done

echo $sum

# Main program starts here

echo "Enter a number:"

read number

# Call the function to find the sum of digits

sum=$(sum_of_digits $number)

# Display the result


echo "The sum of digits of the number is: $sum"

You might also like