Saturday, 16 July 2016

Write a shell script in Linux/Unix to find the biggest number in an array.


echo "ENTER THE NUMBER OF ELEMENTS-"
read e
i=0
while test $i -le `expr $e - 1`
do
echo "ENTER $i ELEMENT-"
read a[i]
i=`expr $i + 1`
done
#SEARCHING THE BIGGEST
lar=${a[0]}
i=1
while test $i -le `expr $e - 1`
do
if test ${a[i]} -gt $lar
then
lar=${a[i]}
fi
i=`expr $i + 1`
done
echo "THE LARGEST NUMBER IS-" $lar


OUTPUT:

ENTER THE NUMBER OF ELEMENTS-
3
ENTER 0 ELEMENT-
6
ENTER 1 ELEMENT-
8
ENTER 2 ELEMENT-
12
THE LARGEST NUMBER IS-12









Write a shell script in Linux/Unix to find the Reverse of a number and check whether the number is Palindrome or not.


echo "ENTER A NUMBER-"
read n
rev=0
t=$n
while test $n -ne 0
do
r=`expr $n % 10`
rev=`expr $rev \* 10`
rev=`expr $rev + $r`
n=`expr $n / 10`
done
echo "THE REVERSE IS-$rev"
if test $rev -eq $t
then
echo "THE NUMBER IS PALINDROME"
else
echo "THE NUMBER IS NOT PALINDROME"
fi


OUTPUT:

ENTER A NUMBER-
121
THE REVERSE IS-121
THE NUMBER IS PALINDROME







Write a shell script in Linux/Unix to solve the expression--x2+7x+12=0


tput clear
echo THE expression is-X^2+7X+12=0
echo roots are---
a=1
b=7
c=12
p=`expr $b \* $b`
s=`expr $a \* $c`
v=`expr $s \* 4`
k=`expr $p - $v`
m=`echo $k ^ 0.5 |bc`
l=`echo -$b + $m |bc`
l1=`echo -$b - $m |bc`
o=`expr 2 \* $a`
x1=`echo $l / $o |bc`
x2=`echo $l1 / $o |bc`
echo X1= $x1
echo X2= $x2


OUTPUT:

THE expression is-X^2+7X+12=0
roots are---
X1= -3
X2= -4









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









Write a shell script in Linux/Unix to find out the value of nCr.


clear
echo “ Enter values for n & r”
read n
read r
t=0
i=1
a=1
b=1
c=1
d=0
while [ $i –le $n ]
do
a=`expr $a \* $i`
i=`expr $i + 1`
done
i=1
while [ $i –le $r ]
do
b=`expr $b \* $i`
i=`expr $i + 1`
done
i=1
d=`expr $n - $r`
while [ $i –le $d ]
do
c=`expr $c \* $i`
i=`expr $i + 1`
done
i=`expr $b \* $c`
t=`expr $a / $i`
echo “ The Result Is = $t”

OUTPUT:

Enter values for n & r
4
2
The Result Is = 6



















Write a shell script in Linux/Unix to find Factorial of a given number.


echo "ENTER A NUMBER-"
read n
fct=1
i=1
while test $i -le $n
do
fct=`expr $fct \* $i`
i=`expr $i + 1`
done
echo "THE FACTORIAL OF THE NUMBER IS -" $fct


OUTPUT:

ENTER A NUMBER-
5
THE FACTORIAL OF THE NUMBER IS-120







Write a shell script in Linux/Unix to generate Fibonacci series.


tput clear
echo enter the range of the fibonacci series----
read n
echo -------------------------------------------------
a=0
b=1

i=2
echo $a
echo $b
while test $i -lt $n
do

c=`expr $a + $b`
echo $c
a=$b
b=$c
i=`expr $i + 1`
done


OUTPUT:

enter the range of the fibonacci series----
9
-------------------------------------------------
0
1
1
2
3
5
8
13
21











Popular Posts