Linux Shell Script to count no of occurrences of a digit in a number
This Code is a Linux shell script to count no of occurrences of a digit in a number
.So What Exactly this Program Does. This Shell Script will first accept Ask the user to Enter A number .Then it Asks the user to Enter the digit of which the occurrence is to be found. then it displays the no of occurrences in the number.
for example if the user Enters a number "34565" and he wants to find how many times the digit "5" is repeated in the number "34565" so he executes this piece of codes and gets the answer as "2"
Code is Given below (save it as occur.sh)
___________________________________________________________________________________________
#to count the occurrences in a give n digit__________________________________________________________________________________
c=0
echo "Enter Number "
read num
x=$num
echo "Enter the digit whose occurance has to be found "
read digit
while [ $num -ne 0 ]
do
rem=`expr $num % 10`
if [ $rem -eq $digit ]
then
c=`expr $c + 1`
fi
num=`expr $num / 10 `
done
echo "No of Occurance of "$digit " is "$c" times"
If you Execute this Code you will get the output
try this out.....Dont Forget to Leave Comments
Comments
Post a Comment