Unix Program
Unix Program
N
O
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Pg
No
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Page 1
28
29
30
31
32
33
23
24
25
26
27
28
29
30
32
33
34
35
36
37
Page 2
34
35
36
37
38
39
40
41
42
38
39
40
42
43
44
44
46
48
-->$ vi welcome.sh
write the code below in it
---------------------------------------------------------------------------------------echo "Welcome to Unix"
echo Enter X
read x
echo x = $x
Page 3
-->$ sh welcome.sh
----------------------------------------------------------------------------------------Output :
Welcome to Unix
Enter X
34
X = 34
09
uday
99
satish
40
Name
satya
uday
satish
Marks
09
99
40
Output :
Page 6
Output :
Enter P,N and R
1000
5
2
Simple Intrest is 100
Page 7
10
Enter side
20
Area of Circle 78.5
Area of Rectangle 200
Area of Square 400
Output :
Enter n
34
n is even
Enter n
37
n is odd
Page 10
Output :
Enter n
34
n is positive
Page 11
Output :
Enter any 3 numbers
36
37
Page 12
34
37 is Great
Page 13
Page 14
Output:
1-10 Natural No's are :
1
2
3
4
5
6
7
8
9
10
Page 15
Page 16
Output:
Enter a Number
123
Reverse of it is 321
Page 17
Output :
Enter a no
28
28 is perfect
Enter a no
34
34 is not perfect
Page 18
Page 19
Output :
Enter n
34
Multiplication Table
34 * 1 = 34
34 * 2 = 68
34 * 3 = 102
34 * 4 = 136
34 * 5 = 170
34 * 6 = 204
34 * 7 = 238
34 * 8 = 272
34 * 9 = 306
34 * 10 = 340
Page 20
read n
sum=0
r=0
x=$n
while [ $x -gt 0 ]
do
r=`expr $n % 10`
sum=`expr $sum + $r \* $r \*r`
x=`expr $n / 10`
done
if [ $n -eq $sum ]
then
echo $n " is Armstrong"
else
echo $n " is not Armstrong"
fi
Output :
Enter a no
153
153 is Armstrong
read n
f=1
while [ $n -gt 0 ]
do
f=`expr $f \* $n`
n=`expr $n - 1`
done
echo "Factorial is " $f
Output :
Enter n
5
Factorial is 120
OUTPUT
Enter Basic Salary
100000
DA is 10000
HRA is 120000
PF is 80000
Gross Salary is 230000
Net Salary is 150000
read num[$i]
done
echo "Sum till a negative no : "
s=0
for ((i=0;i<n;i++))
do
if [ num[$i] -gt 0 ]
then
s=`expr $s + $num[$i]`
else
break
fi
done
echo $s
Output :
Enter length of list
5
Enter list of Numbers
10
40
-34
50
-36
Sum till a negative no :
50
#27. To implement Continue statement
declare -a num[]
echo "Enter length of list"
read n
i=0
echo "Enter list of Numbers"
for ((i=0;i<n;i++))
do
read num[$i]
Page 28
done
echo "Sum of all positive nos : "
s=0
for ((i=0;i<n;i++))
do
if [ num[$i] -gt 0 ]
then
s=`expr $s + $num[$i]`
else
continue
fi
done
echo $s
Output :
Enter length of list
5
Enter list of Numbers
10
40
-34
50
-36
Sum of all positive nos :
100
#28. To print the corresponding day Using else if ladder
echo Enter day no.
read d
if [ $d eq 1 ]
then
echo Sunday
else
if [ $d eq 2 ]
then
Page 29
echo Monday
else
if [ $d eq 3 ]
then
echo Tuesday
else
if [ $d eq 4 ]
then
echo Wednesday
else
if [ $d eq 5 ]
then
echo Thursday
else
if [ $d eq 6 ]
then
echo Friday
else
if [ $d eq 7 ]
then
echo Saturday
else
echo Enter correct day no. between 1-7
fi
fi
fi
fi
fi
fi
fi
OUTPUT
Enter day no. 2
Tuesday
Page 30
echo Tuesday
elif [ $d eq 4 ]
then
echo Wednesday
elif [ $d eq 5 ]
then
echo Thursday
elif [ $d eq 6 ]
then
echo Friday
elif [ $d eq 7 ]
then
echo Saturday
else
echo Enter the day no. between 1-7
fi
OUTPUT
Enter day no. 2
Tuesday
#30. To print the corresponding day Using case control
structure
echo Enter day no.
read d
case $d in
1)echo Sunday
;;
2)echo Monday
;;
3)echo Tuesday
;;
4)echo Wednesday
Page 32
;;
5)echo Thursday
;;
6)echo Friday
;;
7)echo Saturday
;;
*)echo Enter the day no. between 1-7
esac
OUTPUT
Enter day no. 2
Tuesday
e=`expr $e + 1`
fi
num=`expr $num / 10`
done
echo "No of odds : "$o
echo "No of evens : "$e
Output :
Enter a number
13439
No of odds : 4
No of evens : 1
;;
3)date
;;
*)break
esac
echo "Do u wish to Continue (y/n)"
read ch
done
OUTPUT :
Enter u r choice --1)No of Users Logged into System
2)Print Calendar for current year
3)Print the date
4)Exit
1
5
Do u wish to Continue (y/n)
N
#34. To perform all Arithmetic operations using Command
line argument
echo "The first no. is " $1
echo "The 2nd no. is " $2
c=`expr $1 + $2`
echo "sum : "$c
c=`expr $1 - $2`
echo "diff : "$c
c=`expr $1 \* $2`
echo "mul : "$c
c=`expr $1 / $2`
echo "div : "$c
c=`expr $1 % $2`
echo "modulus : "$c
Page 36
Output :
sh add.sh 36 34
The first no. is 36
The 2nd no. is 34
sum : 70
diff : 2
div : 1
mul : 1224
modulus : 2
Page 38
then
c=`expr $c + 1`
fi
done
if [ $c -gt 0 ]
then
echo "It is in the List"
else
echo "It is not in the list"
fi
Output :
Enter length of list
5
Enter list of Numbers
10
40
30
20
50
Enter element to be searched
30
It is in the List
Page 40
Page 41
moo moo
bow bow
meow meow
buzz
blurrrr
for(i=0;i<n;i++)
{
tt[i]=bt[i]+wt[i];
at+=tt[i];
printf("\n\n\tp%d\t\t%d\t\t%d\t\t%d",i,bt[i],wt[i],tt[i]);
}
printf("\n\n\t average waiting time is %f",sum/n);
printf("\n\n\t average turn around time %f",at/n);
getch();
}
Output :
enter the no of process 4
enter the burst time of each process
p0 1
p1 4
p2 8
p3 3
process name
around time
burst time
waiting time
p0
p1
p2
13
p3
13
turn
16
Page 44
41. /*SJF*/
#include <stdio.h>
void main()
{
int i=0,pno[10],bt[10],n,wt[10],temp=0,j,tt[10];
float sum,at;
clrscr();
printf("\n enter the no of process ");
scanf("\n %d",&n);
printf("\n enter the burst time of each process");
for(i=0;i<n;i++,pno[i]=1){
printf("\n p%d",i);
scanf("%d",&bt[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(bt[i]>bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pno[i];
pno[i]=pno[j];
pno[j]=temp;
Page 45
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
sum=sum+wt[i];
}
printf("\n process no \t burst time\t waiting time \t turn
around time\n");
for(i=0;i<n;i++){
tt[i]=bt[i]+wt[i];
at+=tt[i];
printf("\n p%d\t\t%d\t\t%d\t\t%d",i,bt[i],wt[i],tt[i]);
}
printf("\n\n\t average waiting time%f\n\taverage turn
around time%f",sum/n,at/n);
getch();
}
Output:enter the no of process 5
enter the burst time of each process
p0 1
p1 5
p2 2
p3 3
p4 4
process no
time
burst time
waiting time
turn around
Page 46
p0
p1
p2
p3
p4
1
2
3
4
5
0
1
3
6
10
1
3
6
10
15
bt[i]=bt[j];
bt[j]=temp;
}
}
}
wt[0]=0;
for(i=1;i<n;i++) {
wt[i]=bt[i-1]+wt[i-1];
sum+=wt[i]; }
printf("\n \t process no \t burst time \t waiting time \t turn
around time");
for(i=0;i<n;i++) {
tt[i]=bt[i]+wt[i];
at +=tt[i];
printf("\n \t p%d \t %d \t %d \t %d",pno[i],bt[i],wt[i],tt[i]);
}
printf("\n \n \t average waiting time: %f \n \t average turn
around time: %f",sum/n,at/n);
getch();
}
Output :
enter
4
enter
p1
p2
p3
p4
process no
around time
p3
p1
burst time
8
5
waiting time
0
8
turn
8
13
Page 48
p2
p4
2
1
13
15
15
16
Page 49