Friday 27 March 2020

Shell program to find the sum of the square of the digits of a number

PROGRAM
echo "Enter the Number :"
read number
n=$number
sum=0
while test $number -gt 0
do
    rem=` expr $number % 10 `
    sum=` expr $sum + $rem \* $rem `
    number=` expr $number / 10 `
done
echo "The Sum Of Square Of Digits in the Given Number $n is $sum"



OUTPUT
Enter the Number :
12
The Sum Of Square Of Digits in the
Given Number 12 is 5

Shell program to find the sum of odd digits and even digits from a number.

PROGRAM
echo "enter a number"
read n
os=0
es=0
while test $n -gt 0
do
  r=`expr $n % 10`
  if test `expr $r % 2` -eq 0
  then
     es=`expr $es + $r`
  else
     os=`expr $os + $r`
  fi
  n=`expr $n / 10`
done
echo "sum of odd digits in the number = $os"
echo "sum of even digits in the number = $es"


OUTPUT
enter a number
12345
sum of odd digits in the number = 9
sum of even digits in the number = 6

Write a shell script to count the number of occurrences of a particular digit in an 8 digit number input. 

PROGRAM
echo enter an 8 digit number
read d
echo enter a digit to find
read f
n=8
for((i=1;i<=n;i++))
do
      k=`echo $d|cut -c $i`
       if test $f -eq $k
       then
            cnt=`expr $cnt + 1`
        fi
done
echo the number of occurrences of number $f is $cnt





OUTPUT
enter an 8 digit number
12323332
enter a digit to find
2
the number of occurrences of number 2 is 3