Saturday, 16 July 2016

Write a shell script in Linux/Unix to solve the following---S=1.2+2.3+3.4+4.5+.....


tput clear
echo the sum of the series---1.2+2.3+3.4+4.5......
echo--------------------------------------------------
echo enter the no of element
read n
s=0.1
sum=0
i=0
while test $i -lt $n
do
s=`echo $s + 1.1 |bc` 
i=`expr $i + 1`
sum=`echo $sum + $s |bc`
done 
echo $sum


OUTPUT:

the sum of the series---1.2+2.3+3.4+4.5......
--------------------------------------------------
enter the no of element
5
17.0







Write a shell script in Linux/Unix to find the sum of squares of n natural numbers.


tput clear
echo ENTER THE NUMBER OF ELEMENTS-
read n
i=1
sum=0
while test $i -le $n
do
p=`expr $i \* $i`
sum=`expr $sum + $p`
i=`expr $i + 1`
done
echo $sum


OUTPUT:

ENTER THE NUMBER OF ELEMENTS-
5
55



Write a shell script in Linux/Unix to find sum of Digits of a number.


echo "ENTER A NUMBER-"
read n
sum=0
while test $n -ne 0
do
dig=`expr $n % 10`
sum=`expr $sum + $dig`
n=`expr $n / 10`
done
echo "THE SUM OF DIGITS OF $n IS- $sum" 


OUTPUT:

ENTER A NUMBER- 255
THE SUM OF DIGITS OF 255 IS-12









Popular Posts