Unix Lab Programs: Exercise No:2. ODD OR EVEN NUMBERS
Unix Lab Programs: Exercise No:2. ODD OR EVEN NUMBERS
Unix Lab Programs: Exercise No:2. ODD OR EVEN NUMBERS
OUTPUT
$ sh odd
Enter numnber : 3
3 is odd number
$sh odd
Enter numnber : 2
2 is even number
OUTPUT
$ sh big
Please enter three numbers (separate number by space) : 1 2 3
Result : 3
$sh big
Please enter three numbers (separate number by space) : 2 3 d
Result : Can not guess greaters of three numbers
EXERCISE NO 5 SQUARE AND CUBE OF FIVE NUMBERS
$ cat > cube
echo " cube and square of first three numbers are"
for i in 1 2 3 4 5
do
sq=`expr $i '*' $i`
cube=`expr $sq '*' $i`
echo "$i $sq $cube"
done
OUTPUT
$ cat fact
echo "Enter a number: "
read num
i=2
res=1
if [ $num -ge 2 ]
then
while [ $i -le $num ]
do
res=`expr $res \* $i`
i=`expr $i + 1`
done
fi
echo "Factorial of $num = $res"
num = $res"
OUTPUT :
$ sh fact
Enter a number:3
Factorial of 3=6
Exercise no: 6B:FIBBANACI SERIES
$ cat fib
echo "Enter How many numbers:"
read num
num1=0
num2=1
echo "Fibonacci series:"
echo $num1
echo $num2
count=2
while [ $count -le $num ]
do
num3=`expr $num1 + $num2`
echo $num3
num1=$num2
num2=$num3
count=`expr $count + 1`
done
Output :
$ sh fib
Enter How many numbers:
3
Fibonacci series:
0
1
1
Exercise no: 6C:PALINDROME OF A NUMBER
$ cat pal
echo -n "Enter number : "
read n
# store single digit
sd=0
# store number in reverse order
rev=""
# store original number
on=$n
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 )) # get next digit
# store previous number and current digit in reverse
rev=$( echo ${rev}${sd} )
done
if [ $on -eq $rev ];
then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi
OUTPUT$ sh pal
Enter number : 333
Number is palindrome
[mohana@localhost mohana]$ sh pal
Enter number : 123
Number is NOT palindrome
OUTPUT
$sh awk1
Welcome to MyAddtion calculation awk program
2+3
2+3=4
EXERCISE NO11:SHELL SCRIPT USING SECURITY COMMAND
$ cat > sec
echo" enter a file name"
read fname
ls -l $fname
echo " do u want to xecute"
read ch
if [ $ch = y]
then
chmod 777 $fname
echo " permission given"
ls -l $fname
else
echo"permission not given"
fi
OUTPUT
$ cc signal.c
$./a.out
message appears in four seconds
Welcome back after four seconds
#include <stdio.h>
void main()
{
FILE *fopen(), *fp;
int c ;
char filename[40] ;
printf("Enter file to be displayed: ");
gets( filename ) ;
fp = fopen( filename, "r");
c = getc( fp ) ;
while ( c != EOF )
{
putchar(c);
c = getc ( fp )
}
fclose( fp );
}