LSP Lab Solutions
LSP Lab Solutions
Steps
enter three numbers
if a > b and a>c then print a
else , if b > a and b > c then print b
else print c
Program
# take a numbers from the user
echo "Enter a number: "
read a
read b
read c
Linux program to input angles of a triangle and find out whether it is valid triangle or not
Steps
Input all three angles of triangle in some variable say angle1, angle2 and angle3.
Find sum of all three angles, store sum in some variable say sum = angle1 + angle2 + angle3.
Check if(sum == 180) then, triangle can be formed otherwise not. In addition, make sure
angles are greater than 0 i.e. check condition for angles if(angle1 != 0 && angle2 != 0 &&
angle3 != 0)
Program
echo "enter angle A"
read A
echo "enter angle B"
read B
echo "enter angle C"
read C
# sum all three angles
d=$((A+B+C))
if [ $A -eq 0 -o $B -eq 0 -o $C -eq 0 ]
then
echo "Enter angles greater than zero"
else
if [ $d == 180 ];
then
echo "valid traingle"
else
echo "not a valid traingle"
fi
fi
Output :
Linux program to check whether a character is alphabet, digit or special character
Steps
A character is alphabet if it in between a-z or A-Z .
A character is digit if it is in between 0-9 .
A character is special symbol character if it neither alphabet nor digit.
Program
echo "enter a char"
read c
#set specified is [a-z], which is a shorthand way of
typing [abcdefghijklmnopqrstuvwxyz]
#set specified is [0-9], which is a shorthand way of typing [0123456789]
if [[ $c == [A-Z] ]];
then
echo "you have entered an alphabet"
elif [[ $c == [a-z] ]];
then
echo "you have entered an alphabet"
elif [[ $c == [0-9] ]];
then
echo "you have entered is a digit"
else
echo "you have entered a special symbols!"
fi
Output
fi
if [ $cp -gt $sp ]
then
s=$((cp - sp))
echo "loss of rs:$s"
else
s=$((sp - cp))
echo "profit of rs:$s"
fi
Output
Second method
for VARIABLE in 1 2 3 4 5 .. N
do
command1
command2
commandN
done
Example
for i in 1 2 3 4 5
do
echo "Welcome "
done
Output
While Loop
while command
do
Statements
done
Example
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
Output
Write a shell script to print all even and odd number from 1 to 10
The expr command in Unix evaluates a given expression and displays its corresponding
output. It is used for: Basic operations like addition, subtraction, multiplication, division, and
modulus on integers. Evaluating regular expressions, string operations like substring, length of
strings etc.
Program
echo "enter n value as range to calculate odd and even numbers."
read n
i=1
while [ $i -le $n ]
do
if [ `expr $i % 2` -eq 0 ]
then
echo even=$i
else
echo odd=$i
fi
i=`expr $i + 1`
done
Output
Write a shell script to print table of a given number
Program
echo "Enter a Number"
read n
i=1
while [ $i -le 10 ]
do
echo " $n x $i = $(( n * i ))"
i=$(( i + 1 ))
done
Output
Write a shell script to calculate factorial of a given number
Program
echo "Enter a number"
read n
f=1
while [ $n -gt 1 ]
do
f=$((f * n)) #f = f * n
n=$((n - 1)) #n= n - 1
done
echo $f
Output
Use of case structure and use of break in shell scripting
Case structure
Shell supports case...esac statement which handles exactly this situation, and it does so more
efficiently than repeated if...elif statements.
The case statement allows you to easily check pattern (conditions) and then process a
command-line if that condition evaluates to true.
In other words the $variable-name is compared against the patterns until a match is found.
*) acts as default and it is executed if no match is found.
The pattern can include wildcards.
You must include ;; at the end of each command-n. The shell executes all the statements up
to the two semicolons that are next to each other.
The esac is always required to indicate end of case statement.
Syntax
case word in
pattern1)
Statement(s) to be executed if pattern1 matches
;;
pattern2)
Statement(s) to be executed if pattern2 matches
;;
pattern3)
Statement(s) to be executed if pattern3 matches
;;
*)
Default condition to be executed
;;
esac
Example
echo "Enter a number"
read num
case $num in
[0-9])
echo “you have entered a single digit number”
;;
[1-9][1-9])
echo “you have entered a two-digit number”
;;
[1-9][1-9][1-9])
echo “you have entered a three-digit number”
;;
*)
echo “your entry does not match any of the conditions”
;;
esac
Output
break statement
break command is used to terminate the execution of for loop, while loop and until loop. It
can also take one parameter i.e.[N]. Here n is the number of nested loops to break. The default
number is 1.
Example
for i in `seq 1 10`
do
if(($i ==5))
then
break
fi
echo $i
done
Output
Write a shell script to make a basic calculator which performs addition, subtraction, division
and multiplication
Program
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
read c
case $c in
1)echo "sum $((a + b))" ;;
2)echo "subtraction $((a - b ))" ;;
3)echo "multiplication $((a * b))" ;;
4)echo "division $((a / b))" ;;
*) echo "enter valid operation"
esac
Output
Nested Functions
One of the more interesting features of functions is that they can call themselves and also
other functions. A function that calls itself is known as a recursive function.
Example
f1 () {
echo "first function "
f2
}
f2 () {
echo "second function"
}
res=`one`
echo "$res"
Output
#You can move the cursor to a specific row and column using tput cup. Following
example positions the cursor at row i and column j.
tput cup $i $j
echo "*"
fi
done
done
Output
Concept of arrays
Array : An array is defined as finite ordered collection of homogenous data, stored in
contiguous memory locations.
finite means data range must be defined.
ordered means data must be stored in continuous memory addresses.
homogenous means data must be of similar data type.
Declaration of Array :
Syntax : data_type array_name[array_size];
Example : int A[5];
Initialization of an Array :
Syntax : data-type array-name[size] = { list of values };
Example : int A[4]={ 67, 87, 56, 77 };
Program :
#include<stdio.h>
#include<conio.h>
void main ()
{
int i, j,temp;
int a[10] = { 1, 9, 7, 10, 2, 44, 11, 99, 20, 24};
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Sorted List ...\n");
for(i = 0; i<10; i++)
{
printf("%d ",a[i]);
}
}
Output :
Two-Dimension Array :
Declaration of two dimensional Array :
Syntax : data_type array_name[rows][columns];
Example : int A[4][3];
Initialisation of two dimensional Array :
Syntax : data-type array-name[no of rows][no of columns] = { list of values };
Example : int A[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
}
Output :
void main()
{
int arr1[100];
int n, i, j, tmp;
printf("\n\nsort elements of array in ascending order :\n ");
printf("Input the size of array : ");
scanf("%d", &n);