She'll Programs
She'll Programs
read r
area=$(echo “scale=2;3.14*($r*$r)”|bc)
d=$(echo “scale=2;2*$r”|bc)
c=$(echo “scale=2;3.14*$d”|bc)
OUTPUT:
$ sh area.sh
Enter the Number
5
The area of the circle is 78.5
The circumference of the circle is 31.4
Ex. No: 4b ROOTS OF QUADRATIC EQUATION
SHELL PROGRAM:
$vi roots.sh
OUTPUT:
read word
OUTPUT:
computer
3 vowels
SHELL PROGRAM – CONDITIONAL STATEMENTS
read i
if [ ‘expr $i % 4’ –eq 0 ]
then
else
OUTPUT:
$ sh leap.sh
Enter a year
1999
$ sh leap.sh
Enter a year
2016
sum=0
echo “Enter the five subject marks for the student”
read m1 m2 m3 m4 m5
sum1=`expr $m1 + $m2 + $m3 +$m4 + $m5`
echo “ sum of 5 subjects are: $sum1”
per=`expr $sum1 / 5`
echo “percentage: “ $per
if [ $per –ge 60 ]
then
echo “ you got distinction”
elif [ $per –ge 50 ]
then
echo “ you got second class”
else
echo “ you got fail”
fi
OUTPUT:
$ sh grade.sh
Enter the five subject marks for the student
85 82 81 83 84
sum of 5 subjects are: 415
percentage:83
you got distinction
Ex. No: 5c GREATEST OF THREE NUMBERS
OUTPUT:
$ sh big.sh
Enter Three Numbers
586
8 is Greater
Ex. No. 5d CASE STATEMENTS
OUTPUT:
$sh case.sh
Type one of the following:
1 - who am I?
2 - who is logged on?
3 - date
4 - calendar
3
Thu Mar 14 16:58:23 IST 2016
SHELL PROGRAM – LOOPING STATEMENTS
odd=0
i=0
even=0
echo "Enter number of elements"
read num
while [ $i -ne $num ]
do
echo "Enter number"
read n
if [ `expr $n % 2` -ne 0 ]
then
odd=`expr $odd + $n`
else
even=`expr $even + $n`
fi
i=`expr $i + 1`
done
echo "Sum of odd numbers $odd"
echo "Sum of even numbers $even"
OUTPUT:
$ sh su.sh
Enter number of elements
4
Enter number
10
Enter number
11
Enter number
12
Enter number
13
Sum of odd numbers 24
Sum of even numbers 22
Ex. No: 6b PALINDROME
SHELL PROGRAM:
$ vi pali.sh
OUTPUT:
$ sh pali.sh
Enter the string
malayalam
It is a palindrome
$ sh pali.sh
Enter the string
unix
It is not a palindrome
Ex.No:6c ARMSTRONG OR NOT
SHELL PROGRAM:
$ vi arm.sh
echo "Enter a Number"
read num
x=$num
sum=0
while [ $num -gt 0 ]
do
y=`expr $num % 10`
z=`expr $y \* $y \* $y`
sum=`expr $sum + $z`
num=`expr $num / 10`
done
if [ $x -eq $sum ]
then
echo "$x is an armstrong Number"
else
echo "$x is not an armstrong Number"
fi
OUTPUT:
$ sh arm.sh
Enter a Number
153
153 is an armstrong Number
$ sh arm.sh
Enter a Number
113
113 is not an armstrong Number
Ex.No:6d FACTORIAL OF A NUMBER
SHELL PROGRAM:
$vi fact.sh
echo "Enter a Number"
read n
i=`expr $n - 1`
p=1
while [ $i -ge 1 ]
do
n=`expr $n \* $i`
i=`expr $i - 1`
done
echo "The Factorial of the given Number is $n"
OUTPUT:
[cs11012@localhost~]$ sh fact.sh
Enter a Number
5
The Factorial of the given Number is 120
Ex.No.6e FIBONACCI SERIES
SHELL PROGRAM:
$ vi fibonnaci.sh
echo "Enter the Limit"
read n
i=2
echo "Fibonacci Series"
echo "----------------"
a=0
b=1
echo $a
echo $b
while [ $i -lt $n ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
i=`expr $i + 1`
done
OUTPUT:
[me11012@localhost~]$ sh fibonnaci.sh
Enter the Limit
3
Fibonacci Series
----------------------
0
1
1
2
Ex.No.6f CHECKING FOR PRIME NUMBER
SHELL PROGRAM:
$ vi prime.sh
echo "Enter a Number"
read n
i=`expr $n - 1`
t=0
while [ $i -ge 2 ]
do
p=`expr $n % $i`
if [ $p -eq 0 ]
then
t=`expr $t + 1`
fi
i=`expr $i - 1`
done
if [ $t -gt 0 ]
then
echo "The Number $n is not a Prime Number"
else
echo "The Number $n is a Prime Number"
fi
OUTPUT:
$ sh prime.sh
Enter a Number
2
The Number 2 is a Prime Number
$ sh prime.sh
Enter a Number
9
The Number 9 is not a Prime Number"
Ex.No.6g SMALLEST AMONG ‘N’ NUMBERS
read n
i=0
temp=100
while [ $i -lt $n ]
do
read a
if [ $temp -gt $a ]
then
temp=$a
fi
i=`expr $i + 1`
done
OUTPUT:
SHELL PROGRAM:
$vi smdig,sh
read n
min=9
t=0
while [ $n -ne $t ]
do
rem=`expr $n % 10`
n=`expr $n / 10`
then
min=$rem
fi
done
OUTPUT:
$ sh smdig.sh
256