#  >> K-12 >> Mathematics

Write a Shell program to find the smallest digits of number?

Here's a simple Shell program to find the smallest digit of a number:

```shell

#!/bin/bash

Function to find the smallest digit of a number

find_smallest_digit() {

# Convert the number to a string

number=$1

# Initialize the smallest digit with the first digit of the number

smallest_digit=${number:0:1}

# Iterate over the remaining digits of the number

for (( i=1; i<${#number}; i++ )) {

# Get the current digit

current_digit=${number:i:1}

# Update the smallest digit if the current digit is smaller

if [ $current_digit -lt $smallest_digit ]; then

smallest_digit=$current_digit

fi

}

# Return the smallest digit

echo $smallest_digit

}

Get the number from the user

read -p "Enter a number: " number

Find the smallest digit of the number

smallest_digit=$(find_smallest_digit $number)

Print the smallest digit

echo "The smallest digit of $number is $smallest_digit."

```

Learnify Hub © www.0685.com All Rights Reserved