Wednesday, 28 March 2018

Shell script to count the number of occurrences of a given digit in a number



#Shell script to count the number of occurrences of a given digit in a number
count=0
echo "enter the number"
read n
num=$n
echo "Enter the number to be checked"
read f
while [ $n -gt 0 ]
do
    m=`expr $n % 10 `
    if [ $m -eq $f ];then
           count=`expr $count + 1`
    fi
    n=`expr $n / 10 `
done
echo "The number $f has occurred $count times in the number $num"


OUTPUT
csc@csc-SVE1513CYNB ~ $ sh occur.sh
enter the number
12131425 
Enter the number to be checked
1
The number 1 has occurred 3 times in the number 12131425

No comments:

Post a Comment