(2009 – 2010)
-:PROGRAM TO ADD AND MULTIPLY TWO NUMBERS:-
SOURCE CODE
echo Enter the value of A:
read A
echo Enter the value of B:
read B
C = ’expr $A+ $B’
echo The sum of A and B is $C
C = ’expr $A\* $B’
echo The product of A and B is $C
OUTPUT
Enter the value of A: 25
Enter the value of B: 20
The sum of A and B is 45
The product of A and B is 500
-:PROGRAM TO CALCULATE THE FACTORIAL OF A NUMBER:-
SOURCE CODE
echo Enter a number:
read n
i=1
f=1
while [$i –le $n]
do
f = ’expr $f \* $i ‘
i = ‘ expr $i + 1 ’
done
echo Factorial of $n is $f
OUTPUT
Enter a number: 5
Factorial of 5 is 120
-:PROGRAM TO CHECK A NUMBER WHEATHER IT IS A PRIME NUMBER OR NOT:-
SOURCE CODE
echo Enter an integer number:
read num
o= ‘ expr $num/2 ‘
flag = 0
i=2
while [ $i –le $o]
do
m = ’expr $num % $i ‘
if[$m –eq 0]
then
flag = 1
fi
i = ‘ expr $i + I ’
done
if [ $flag –eq 0]
then
echo The number is a prime number.
else
echo The number is not a prime number.
fi
OUTPUT
Enter an integer number: 11
The number is a prime number.
Enter an integer number: 27
The number is ...
This is a preview of the whole essay
SOURCE CODE
echo Enter an integer number:
read num
o= ‘ expr $num/2 ‘
flag = 0
i=2
while [ $i –le $o]
do
m = ’expr $num % $i ‘
if[$m –eq 0]
then
flag = 1
fi
i = ‘ expr $i + I ’
done
if [ $flag –eq 0]
then
echo The number is a prime number.
else
echo The number is not a prime number.
fi
OUTPUT
Enter an integer number: 11
The number is a prime number.
Enter an integer number: 27
The number is not a prime number.
-:PROGRAM TO CHECK A NUMBER WHEATHER IT IS AN ARMSTRONG NUMBER OR NOT:-
SOURCE CODE
echo Enter a number:
read num
ct = ‘expr $num|wc –c ‘
ct = ‘ expr $ct - 1 ‘
i=0
a = $num
b = $ct
sum = 0
while [ $a –gt 0 ]
do
c = $i
while [ $b –gt 1 ]
do
i = ‘expr $i \* $c ’
b = ‘ expr $b – 1 ‘
done
sum = ‘expr $sum + $i ‘
a = ‘expr $a/10 ‘
b = $ct
done
if [ $num –eq $sum ]
then
echo The number is an Armstrong number.
else
echo The number is not an Armstrong number.
fi
OUTPUT
Enter a number: 153
The number is an Armstrong number.
-:PROGRAM TO CHECK A NAME WHEATHER IT IS A PALINDROME OR NOT:-
SOURCE CODE
echo Enter a name:
read name
cct = ‘ expr $name|wc –c ‘
cct = ‘ expr $cct -1 ‘
flag = 0
i = 1
j = $cct
while [ $i –le $j ]
do
y = ‘ expr $name|cut –c $i ‘
z = ‘ expr $name|cut –c $j ‘
if = [ $i!= $z ]
then
flag = 1
fi
i = ‘ expr $name|cut –c $i ‘
done
if [$flag –eq 1]
then
echo The name is a Palindrome.
else
echo The name is not a Palindrome.
fi
OUTPUT
Enter a name: KANAK
The name is a Palindrome.
-:PROGRAM TO READ A NUMBER AND PRINT THE NUMBER IN WORDS:-
SOURCE CODE
echo Enter a number:
read num
ct = ‘ expr $num|wc –c ‘
ct = ‘ expr $ct -1 ‘
i = 1
p = 2
while [ $i –le $ct]
do
if [ $p –eq $ct ]
then
case $num in
10) echo Ten ;;
11) echo Eleven ;;
12) echo Twelve;;
13) echo Thirteen ;;
14) echo Fourteen ;;
15) echo Fifteen ;;
16) echo Sixteen ;;
17) echo Seventeen ;;
18) echo Eighteen ;;
19) echo Nineteen ;;
*) p = 1
esac
if [ $p –eq 1 ]
then
c = ‘ echo $num|cut –c $i ‘
case $c in
2) 1= ‘Twenty’ ;;
2) 1= ‘Twenty’ ;;
2) 1= ‘Twenty’ ;;
2) 1= ‘Twenty’ ;;
2) 1= ‘Twenty’ ;;
2) 1= ‘Twenty’ ;;
2) 1= ‘Twenty’ ;;
2) 1= ‘Twenty’ ;;
2) 1= ‘Twenty’ ;;
esac
p = 3
fi
if [ $num –le 19]
then
exit
fi
c = ‘ echo $num|cut –c 2 ‘
if [ $c –eq 0 ]
then
exit
fi
else
c = ’ echo $num|cut –c $ct ‘
case $c in
s= ‘Zero’ ;;
s= ‘One’ ;;
s= ‘Two’ ;;
s= ‘Three’ ;;
s= ‘Four’ ;;
s= ‘Five’ ;;
s= ‘Six’ ;;
s= ‘Seven’ ;;
s= ‘Eight’ ;;
s= ‘Nine’ ;;
esac
fi
p = 1
I = ‘expr $i + 1’
done
if [ $ct –eq 2 ]
then
z = ‘echo $1 $s’
echo $z
else
echo $s
fi
OUTPUT
Enter a number: 75
Seventy Five
-:PROGRAM TO PRINT THE FIBONACCI SERIES:-
SOURCE CODE
echo Mention the range that you want to see
read n
i=1
s=1
l=0
while[ $i –le $n]
do
if [ $i –eq 1]
then
echo 0
elif [$i –eq 2]
then
echo 1
else
p=`expr $s + $l`
l=$s
s=$p
echo $p
fi
i=`expr $i + 1`
done
OUTPUT
Mention the range that you want to see – 5
0
1
1
2
3
-:PROGRAM TO FIND THE QUOTIENT AND THE REMAINDER BETWEEN TWO NUMBERS WITHOUT USING ‘/’ AND ‘%’ OPERATOR:-
SOURCE CODE
echo Read the dividend:
read dividend
echo Read the divider:
read divider
i=0
while[ $dividend –ge $divider]
do
dividend=`expr $dividend - $diviser`
i=’expr $i + 1’
done
echo The quotient is $i
echo The remainder is $dividend
OUTPUT
Read the dividend: 17
Read the divider: 5
The quotient is 3
The remainder is 2
-:PROGRAM TO FIND THE GREATEST NUMBER AMONG THE THREE NUMBERS:-
SOURCE CODE
echo Enter the value of A:
read A
echo Enter the value of B:
read B
echo Enter the value of C:
read c
if[$A –gt $B –a $A –gt $C ]
then
echo A is the greatest.
elif[$B –gt $A –a $B –gt $C ]
then
echo B is the greatest.
else
echo C is the greatest.
Fi
OUTPUT
Enter the value of A: 15
Enter the value of B: 20
Enter the value of C: 25
C is the greatest.
-:PROGRAM TO INPUT A FEW NUMBER IN AN ARRAY AND DISPLAY THE NUMBERS WHICH ARE GREATER THAN THE AVERAGE OF ALL THE NUMBERS:-
SOURCE CODE
echo How many numbers do you want to enter?
Read num
I=0
sum=0
while[$i –lt $num]
do
echo Enter the number:
read a[$i]
sum=`expr $sum + ${a[$i]}`
i=` expr $i + 1`
done
avg=`expr $sum/$num`
i=0
echo The numbers are:
while[$i –lt $num]
do
if[${a[$i]} –gt $avg]
then
echo ${a[$i]}
fi
i=`expr $i + 1`
done
OUTPUT
How many numbers do you want to enter? 4
Enter the number: 10
Enter the number: 20
Enter the number: 30
Enter the number: 40
The numbers are: 30
40
-:PROGRAM TO PRINT A WORD IN ALL IT’S PROBABLE OR POSSIBLE WAYS:-
SOURCE CODE
echo Enter a word:
read word
ct=`expr $word | wc -c`
t=`expr $ct - 1`
i=0
k=1
j=1
while[$i –lt $t]
do
wr=”
k=1
j=`expr $j +1`
while[$k –le $t]
do
if[$j –gt $t]
then
j=1
fi
s=`expr $word | cut –c $j`
wr=`echo $wr$s`
j=`expr $j +1`
k=`expr $k +1`
done
echo $wr
i=`expr $i + 1`
done
OUTPUT
Enter a word: INDIA
INDIA
NDIAI
DIAIN
IAIND
AINDI
-:PROGRAM TO CALCULATE THE AGE:-
SOURCE CODE
echo Please enter the date of birth:-
echo Enter the date:
read da
echo Enter the month:
read mo
echo Enter the year:
read yr
d=`date + %d`
m=`date + %m`
y=`date| cut –c 25-28`
if[$d –lt $da]
then
t=`expr $d + 30`
flagdt = 1
cd=`expr $t -- $da`
else
flagdt = 0
cd=`expr $d -- $da`
fi
if[$flagdt –eq 1]
then
mn=`expr $mn + 1`
fi
if[$m –lt $mn]
then
t=`expr $m + 12`
flagmn=1
cm=`expr $m -- $mn`
fi
if[$flagmn –eq 1]
then
yr=`expr $yr +1`
fi
cy=`expr $y -- $yr`
dt=`date + %D`
echo The current date is $dt
echo The calculated age is $cy years $cm months %cd days.
OUTPUT
Please enter the date of birth:-
Enter the date: 04
Enter the month: 09
Enter the year: 1990
The current date is 24/04/11
The calculated age is 20 years 7 months 20 days.
-:PROGRAM TO REVERSE AN INTEGER NUMBER:-
SOURCE CODE
echo Enter the number:
read num
sum=0
while[$num –gt 0]
do
m= `expr $num % 10`
sum= `expr $sum \* 10 + $m`
num= `expr $num / 10`
done
echo The reverse number of the given integer number is $num.
OUTPUT
Enter the number: 143
The reverse number of the given integer number is 341
-:PROGRAM TO FIND THE THE DIVISION ON AVERAGE MARKS:-.
SOURCE CODE
echo Enter the average marks:
read mark
case $mark in
[6-9][0-9]) echo Get first division. ;;
[5][0-9]|[4][5-9]) echo Get second division. ;;
[3][0-9]|[4][0-4]) echo Get third division. ;;
[1-2][0-9]|[0-9]) echo Failed. ;;
Esac
OUTPUT
Enter the average marks:62
Get first division.
Enter the average marks:
Failed.
-:PROGRAM TO FIND THE EXE FILES OF A PWD:-
SOURCE CODE:-
for i in `ls`
do
if[-x $i]
then
echo One of the file is $i
fi
done
OUTPUT:-
One of the file is Numreverse
One of the file is Greatest
One of the file is Numsummul
-:PROGRAM TO SHOW THE CONTENTS OF A FILE IF IT IS READABLE:-
SOURCE CODE
echo Enter a filename:
read file
if[-r $file]
then
echo The content of the file is –
cat $file
else
echo The file has no read permission.
fi
OUTPUT
Enter a filename: exiefiles
The file has no read permission.
Enter a filename: exefiles
The content of the file is –
for i in `ls`
do
if[-x $i]
then
echo One of the file is $i
fi
done
-:PROGRAM TO COUNT THE WRITABLE FILES OF A PWD:-
SOURCE CODE
for i in `ls`
do
if[-w $i]
then
ct= `expr $ct + 1`
fi
done
echo The numbers of write permitted files are $ct
OUTPUT
The numbers of write permitted files are 6
-:PROGRAM TO READ A FILENAME AND SHOW THE CONTENTS OF A READABLE FILE IF THE NAME IS OF A DIRECTORY FILE OR SHOW THE CONTENTS IF IT IS A NORMAL FILE OTHERWISE IF IT IS A BLOCK SPECIAL OR CHARACTER SPECIAL FILE THEN SHOW THAT IT IS A SPECIAL FILE:-
SOURCE CODE
echo Enter a filename:
read file
if[-d $file]
then
m=1
elif[-f $file]
then
m=2
else
m=3
fi
case $m in
- Cd $file
for I in `ls`
do
if[-r $i]
then
echo $i
fi
done
cd
;;
- Cat $file
mv $file Newone
;;
- Echo The file is a special file::
esac
OUTPUT
Enter a filename: writable.sh
for i in `ls`
do
if[-w $i]
then
ct= `expr $ct + 1`
fi
done
echo The numbers of write permitted files are $ct
Page of