Shell Script Algorithms
Shell Script Algorithms
Experimen
t No
3
4
5
6
7
8
9
10
11
12
13
14
Title
CALCULATOR
LARGEST AMONG THREE NUMBERS
VOWEL CHECKING
FIBONACCI SERIES
PRIME NUMBER CHECKING
ARMSTRONG NUMBER SERIES
TEMPERATURE CONVERSION
PALINDROME CHEKING
FACTORIAL -RECURSION
LINEAR SEARCH
SORTING
BINARY SEARCH
Experiment No: 3
CALCULATOR
Aim:
Write a shell script program to read two numbers and perform basic arithmetic
operations( + , - , * , / ,%)
Algorithm :
Step 1: Start
Step 2: Read two integers a, b
Step 3: Calculate Sum= a + b
Diff= a b
Product= a * b
Div=a / b
Rem=a % b
Step 4: Display Sum,Diff,Product,Div and Rem
Step 5: Stop
Result:
The program was written and executed successfully and the desired output is obtained.
Experiment No:4
LARGEST AMONG THREE NUMBERS
Aim:
Write a shell script to read three integer numbers and print the largest among three numbers.
Algorithm :
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a>b
If a>c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b>c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
Result:
The program was written and executed successfully and the desired output is obtained.
Experiment No:5
VOWEL CHECKING
Aim:
Write a shell script program to read a character from keyboard and check whether it is vowel or
not.
Algorithm:
step1: Start
step2: Declare variable ch .
step3: Read the value of ch.
Step4: if (ch==A || ch==a || ch==E || ch==e|| ch==I || ch==i|| ch==O || ch==o ||
ch==U || ch == u ) then
Display Entered character is Vowel
Goto step 6
Step5: Display Entered character is not Vowel goto step 6
Step 6: stop
Result:
The program was written and executed successfully and the desired output is obtained.
Experiment No:6
FIBONACCI SERIES
Aim:
Write a shell script to print out the Fibonacci series up to a limit.
Algorithm:
STEP 1: start
STEP 2: Declare variables n, a 0,b 1, c, i
STEP 3: Read values of n
STEP 4: Display a, b
STEP 5: Assign i2
STEP 6: if i < n then goto step 7 otherwise goto step10
STEP 7: calculate
c a+b,
i i+1
a b, b c
Display the value of c
goto step 6
STEP 10: stop
Result:
The program was written and executed successfully and the desired output is obtained.
Experiment No:7
PRIME NUMBER CHECKING
AIM:
To write a shell script to check whether the given number is prime or not.
ALGORITHM:
STEP 1: Start
STEP 2: Read an integer n
STEP 3: Assign i=2, j=0
STEP 4: Is i < n then r =n % i. otherwise go to step 8
STEP 5: Is r=0 then increment i and j value by i. otherwise go to step 6
Experiment No:8
ARMSTRONG NUMBER SERIES
AIM:
To write a shell script to find the Armstrong numbers between 1 to N.
ALGORITHM:
STEP 1: Start
STEP 2: When i equal to 0 and less than or equal to N, calculate increment value of i.
STEP 3: Assign value of i to temp and n.
STEP 4: Assign value of ams equal to zero.
STEP 5: When n not equal to zero calculate
remn%10;
ams=ams+rem*rem*rem
nn/10
STEP 6: If temp equal to ams then print the value of ams.
STEP 7: Thus for each value of i, values of ams is printed.
STEP 8: Stop the program.
Result:
The program was written and executed successfully and the desired output is obtained.
Experiment No:9
TEMPERATURE CONVERSION
AIM:
Experiment No: 10
PALINDROME CHEKING
Aim:
Write a shell script to read an integer find out the reverse of the integer using function and
check whether integer is palindrome or not.
Algorithm:
Step 1:start
Step2: read n
Step 3: copy n into m for later use. Also, initialize rn;
Step 5:while n is not zero
1. r = n % 10
2. n = n/10
3. rn = rn*10 + r;
Step 6: if m equal rn then the number is palindrome.
Step 7: Else Print number is not palindrome
Step 8: stop
Result:
The program was written and executed successfully and the desired output is obtained.
Experiment No: 11
FACTORIAL -RECURSION
Aim:
Write a shell script to read an integer find out the factorial of the integer.
Algorithm:
step1: start
Step2: Read a number n and fact=1
Step3:if n==1 then
Return( 1)
Step4: else
For i=0 to i<n
Factorial=fact*fact(n-1)
Return(fact)
Step4: stop
Result:
The program was written and executed successfully and the desired output is obtained.
Experiment No: 12
LINEAR SEARCHING
Aim:
Write a shell script program to read an array of n integers and perform linear search
operation.
Algorithm:
Step 1: Start
Step 2: Read the array A of n elements, f=0
Step 3: Read the element x to be searched in A
Step 4: Set i to 0
Step 5: if i > n then go to step 10
Step 6: if A[i] = x then f=1 and go to step 9
Step 7: Set i to i + 1
Step 8: Go to Step 5
Step 9: Print Element x Found at index i+1 and go to step 11
Step 10: if f=0 then Print element not found
Step 11: Stop
Result:
The program was written and executed successfully and the desired output is obtained.
Experiment No: 13
SORTING
Aim:
Write a shell script program to read an array of n integers and sort number in ascending
order using bubble sort technique.
Algorithm:
Step1: Start
Step2 : Read the number of array elements
step3: For i = 0 to n-1
Read array[i]
Step4: For i = 0 to n-1
For j = 0 to n-i-1
If ( array[i]>array[j+1]) then
Temp=array[j]
Array[j]=array[j+1]
Array[j+1]=temp
Step7: Display array elements
Step8:stop
Result:
The program was written and executed successfully and the desired output is obtained.
Experiment No: 14
BINARY SEARCH
Aim:
Write a shell script program to read an array of n integers and perform binary search.
Algorithm :
Step 1: Start
Step 2: Read the array a of n elements, f=0
Step 3: Sort using any algorithm
Step 4: Read the element to be searched in x
Step 5: Set L=0 the lower limit and u=n-1 the upper limit
Step 6: Repeat the steps 7,8,9,10 until u>=L
Step 7: mid =(L+u)/2
Step 8: when a[mid]==x f=1 print the search is successful, display the position goto step 12
Step 9: when a[mid]<x L=mid+1
Step 10:when a[mid]>x u=mid-1
Step 11:if f==0 print search is unsuccessful
Step 12:Stop
Result:
The program was written and executed successfully and the desired output is obtained.