You are on page 1of 10

1) Arithmatic operations

echo enter two numbers :-


read x y
a=`echo $a + $b|bc`
b=`echo $a - $b|bc`
c=`echo $a \* $b|bc`
d=`echo $a / $b|bc`
echo Sum = $a
echo Difference = $b
echo Product = $c
echo Division = $d

2) Calculate Gross Salary


echo Enter Basic Salary :-
read s
da=`echo $s \* 0.8|bc`
hra=`echo $s \* 0.25|bc`
gs=`echo $s + $da + $hra|bc`
echo The Gross Salary = $gs

3) voter
echo Enter your age :-
read age
if [ $age -lt 18 ]
then
echo NOT Eligible
else
echo Eligible
fi

4) largest of three numbers


echo Enter three Numbers :-
read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo Largest = $a
elif [ $b -gt $a -a $b -gt $c ]
then
echo Largest = $b
else
echo Largest = $c
fi

5) To check if a number is even or odd


echo Enter a Number :-
read n
if [ `expr $n % 2` -eq 0 ]
then
echo Even Number
else
echo Odd Number
fi
6) Grade
echo Enter marks of 5 subjects :-
read a b c d e
total=`expr $a + $b + $c + $d + $e`
res=`echo $total \ 5|bc`
if [ $res -ge 90 ]
then
echo Grade = O
elif [ $res -ge 80 ]
then
echo Grade = E
elif [ $res -ge 70 ]
then
echo Grade = A
elif [ $res -ge 60 ]
then
echo Grade = B
elif [ $res -ge 50 ]
then
echo Grade = C
elif [ $res -ge 40 ]
then
echo Grade = D
else
echo Grade = F
fi

7) perform addition, subtraction, multiplication, division using switch case


echo Enter two Numbers :-
read a b
echo Enter 1 for Addition
echo Enter 2 for Subtraction
echo Enter 3 for Multiplication
echo Enter 4 for Division
echo Enter your choice
read ch
case $ch in
1)
add=`expr $a + $b`
echo Addition = $add
;;
2)
sub=`expr $a - $b`
echo Difference = $sub
;;
3)
prod=`echo $a \* $b|bc`
echo Product = $prod
;;
4)
div=`echo $a / $b|bc`
echo Division = $div
;;
*)
echo WRONG CHOICE
;;
esac

8) Different operations
echo Enter 1 to print each number 1 from number 2
echo Enter 2 to print the reverse of a number
echo Enter 3 to print sum of digits of a number
echo Enter 4 to print factorial of a number
echo Enter 5 to print the fibonacci series of n terms
echo Enter your choice
read ch
case $ch in
1)echo Enter two Numbers
read a b
while [ $a -le $b ]
do
echo $a
a=`expr $a + 1`
done
;;

2)echo Enter the number


read n
rev=0
while [ $n -ne 0 ]
do
m=`expr $n % 10`
rev=`expr $rev \* 10 + $m`
n=`expr $n / 10`
done
echo Reverse = $rev
;;

3)echo Enter the number


read n
sum=0
while [ $n -gt 0 ]
do
m=`expr $n % 10`
sum=`expr $sum + $m`
n=`expr $n / 10`
done
echo Sum of digits = $sum
;;

4)echo Enter the number


read n
f=1
i=1
while [ $i -le $n ]
do
f=`expr $f \* $i`
i=`expr $i + 1`
done
echo Factorial = $f
;;

5)echo Enter the nth term


read n
a=0
b=1
i=2
if [ $n -ge 1 ]
then
echo -n Fibonacci Series = $a
echo -n " "
fi
if [ $n -ge 2 ]
then
echo -n $b
echo -n " "
fi
while [ $i -lt $n ]
do
c=`expr $a + $b`
a=$b
b=$c
i=`expr $i + 1`
echo -n $c
echo -n " "
done
;;

*)echo Wrong answer


esac

9) patterns

echo Enter from 1 to 5 to print different patterns :-


read choice
echo Enter the number of lines :-
read line
n=`expr $line - 1`
case $choice in
1)
i=0
j=0
while [ $i -le $n ]
do
j=0
while [ $j -le $i ]
do
echo -n ' * '
j=`expr $j + 1`
done
echo
i=`expr $i + 1`
done
;;
2)
i=0
j=0
while [ $i -le $n ]
do
j=0
while [ `expr $i + $j` -le $n ]
do
echo -n ' * '
j=`expr $j + 1`
done
echo
i=`expr $i + 1`
done
;;
3)
i=0
j=0
while [ $i -le $n ]
do
j=0
while [ $j -le $n ]
do
if [ `expr $i + $j` -lt $n ]
then
echo -n " "
else
echo -n " * "
fi
j=`expr $j + 1`
done
echo
i=`expr $i + 1`
done
;;
4)
i=0
j=0
while [ $i -le $n ]
do
j=0
while [ $j -le $n ]
do
if [ $j -lt $i ]
then
echo -n " "
else
echo -n " * "
fi
j=`expr $j + 1`
done
echo
i=`expr $i + 1`
done
;;
5)

i=0
j=0
m=`expr $n \* 2`
while [ $i -le $n ]
do
j=0
while [ $j -le $m ]
do
if [ `expr $i + $j` -ge $n -a `expr $j - $i` -le $n ]
then
echo -n " * "
else
echo -n " "
fi
j=`expr $j + 1`
done
echo
i=`expr $i + 1`
done
;;
*)
echo WRONG CHOICE!!
esac

10) FCFS

#include<stdio.h>
int main() {
int
n,i=0,burstTime[10],arrivalTime[10],turnAroundTime[10],waitingTime[10],finishTime[10];
int sum=0,sum_TAT=0,sum_WT=0;

printf("Enter the Number of Process :- \n");


scanf("%d",&n);
printf("Enter the Burst Time :- \n");
for(i=0;i<n;i++){
scanf("%d",&burstTime[i]);
}
printf("Enter the Arrival Time :- \n");
for(i=0;i<n;i++){
scanf("%d",&arrivalTime[i]);
}
for(i=0;i<n;i++){
sum += burstTime[i];
finishTime[i]=sum;
turnAroundTime[i] = finishTime[i] - arrivalTime[i];
sum_TAT += turnAroundTime[i];
waitingTime[i] = turnAroundTime[i] - burstTime[i];
sum_WT += waitingTime[i];
}
printf("\n------------------------------------------------\n");
for(i=0;i<n;i++){
printf("P%d\t",i+1);
}
printf("\n------------------------------------------------\n");
for(i=0;i<n;i++){
printf("%d\t",finishTime[i]);
}
printf("\n\nAverage Turn Around Time = %.3f",(float)sum_TAT/n);
printf("\nAverage Waiting Time = %.3f\n",(float)sum_WT/n);
return 0;
}

11) SJF
#include<stdio.h>
int main() {
int n,i=0,j=0,temp1=0,temp2=0,sum=0,sum_TAT=0,sum_WT=0;
int
burstTime[10],arrivalTime[10],waitingTime[10],turnAroundTime[10],finishTime[10],process[10];
printf("Enter the Number of Process : \n");
scanf("%d",&n);
printf("Enter the Burst Time : \n");
for(i=0;i<n;i++) {
scanf("%d",&burstTime[i]);
process[i]=i+1;
}
printf("Enter the Arrival Time : \n");
for(i=0;i<n;i++){
scanf("%d",&arrivalTime[i]);
}
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++) {
if(burstTime[j] > burstTime[j+1]) {
temp1 = burstTime[j];
burstTime[j] = burstTime[j+1];
burstTime[j+1] = temp1;

temp2 = process[j];
process[j] = process[j+1];
process[j+1] = temp2;
}
}
}
for(i=0;i<n;i++) {
sum += burstTime[i];
finishTime[i] = sum;
turnAroundTime[i] = finishTime[i] - arrivalTime[i];
sum_TAT += turnAroundTime[i];
waitingTime[i] = turnAroundTime[i] - burstTime[i];
sum_WT += waitingTime[i];
}
printf("\n----------------------------------------------\n");
for(i=0;i<n;i++)
printf("P%d\t",process[i]);
printf("\n-----------------------------------------------\n");
for(i=0;i<n;i++)
printf("%d\t",finishTime[i]);
printf("\n\nAverage Turn Around Time = %.3f",(float)sum_TAT/n);
printf("\nAverage Waiting Time = %.3f\n",(float)sum_WT/n);
return 0;
}

12) Magic NUmber


echo Enter the number
read n
rev=0
m=$n
sum=0

while [ $m -gt 0 ]
do
x=`expr $m % 10`
sum=`expr $sum + $x`
m=`expr $m / 10`
done
p=$sum
while [ $p -gt 0 ]
do x=`expr $p % 10`
rev=`expr $rev \* 10 + $x`
p=`expr $p / 10`
done

if [ `expr $rev \* $sum` -eq $n ]


then echo $n is a Magic Number
else
echo $n is not a Magic Number
fi

13) Bankers Algorithm


#include<bits/stdc++.h>
using namespace std;
int main() {
int p_num=0,r_num=0;
cout<<"Enter the Number of processes :- \n";
cin>>p_num;
cout<<"Enter the Number of resources :- \n";
cin>>r_num;
int Allocation[p_num][r_num]={0},Max[p_num][r_num]={0};
int Need[p_num][r_num]={0},Available[r_num]={0};
cout<<"Enter the Allocation matrix :-\n";
for(int i=0;i<p_num;i++) {
for(int j=0;j<r_num;j++) {
cin>>Allocation[i][j];
}
}
cout<<"Enter the Max matrix :-\n";
for(int i=0;i<p_num;i++) {
for(int j=0;j<r_num;j++) {
cin>>Max[i][j];
}
}
cout<<"Enter the Available Matrix :- \n";
for(int i=0;i<r_num;i++) {
cin>>Available[i];
}

for(int i=0;i<p_num;i++) {
for(int j=0;j<r_num;j++) {
Need[i][j]=Max[i][j]-Allocation[i][j];
}
}
bool finish[p_num]={false},check=false;
int answer[p_num]={0},index=0;
for(int x=0;x<p_num;x++) {
for(int i=0;i<p_num;i++) {
for(int j=0;j<r_num;j++) {

if((finish[i]==false) && (Need[i][j]<=Available[j])) {


check = true;
}
else {
check=false;
break;
}
}
if(check) {
finish[i] = true;
for(int j=0;j<r_num;j++) {
Available[j] += Allocation[i][j];
}
answer[index] = i;
index++;
}
}
}

bool flag=true;
for(int i=0;i<p_num;i++) {
if(finish[i]==false)
flag=false;
}
if(flag) {
cout<<"The Safe State is : ";
for(int i=0;i<p_num;i++) {
cout<<"P"<<answer[i]+1<<" ";
}
cout<<"\n";
}
else {
cout<<"The system is not in Safe State.\n";
}
return 0;
}

You might also like