II SHELL PROGRAMMING
Addition of two numbers
Algorithm:
Step 1: Start
Step 2: Get the input values
Step 3: Calculate an expression a+b for adding 2 numbers
Step 4: print the output
Step 5: Stop
Program:
echo Enter the numbers
read a b
c=`expr $a + $b`
echo "the sum is $c"
Input :
Enter the numbers
12
Output :
The sum is 24
SQUARE OF A NUMBER
Algorithm:
Step 1 : Start
Step 2: Get the input x
Step 3: Calculate an expression x * x for squaring a number
Step 4: Print the output
Step 5: Stop
Program:
echo enter the number
read x
ans=`expr $x \* $x`
echo "the square value is : $ans"
Input:
Enter the number
5
Output
The square value is 25
AREA & CIRCUMFERENCE OF A CIRCLE
Algorithm:
Step 1: Start
Step 2: Read the value of radius
Step 3: Initialize the ( value as 3
Step 4: Calculate an expressions
Area=(r2
Circumference=2 ( r
Step 5: Print the values of area and circumference
Step 6: Stop
Program:
echo enter the radius
read r
pi=3
area=`expr $pi \* $r \* $r`
circumference=`expr 2 \* $pi \* $r`
echo "the area of the circle is $area"
echo "the circumference of the circle is $circumference"
Input:
Enter the radius
5
Output:
The area of the circle is 78.5
The circumference of the circle is 31.4
BIGGEST AMONG THREE NUMBERS
Algorithm
Step 1: Start
Start 2: Read three values through the variable a,b,c
Start 3: First compare a>b and a>c
Start 4: If the condition is satisfied then print a is biggest
Start 5: If it is not satisfied, compare b>c
Start 6: If it is satisfied print b is biggest otherwise print c is biggest
Start 7: Stop
Program:
echo "enter 3 numbers"
read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo "$a is biggest"
else
if [ $b -gt $c ]
then
echo "$b is biggest"
else
echo "$c is biggest"
fi
fi
Input
Enter 3 numbers
5 8 3
Output
B is biggest
ODD OR EVEN
Algorithm:
Step 1 : Start
Step 2: read the number
Step 3: find the remainder value of that number
Step 4: If the remainder value is 0, the given number if even
Step 5: Otherwise conclude that the given number is odd
Step 6: Stop
Program:
echo "enter the number"
read a
if [ `expr $a % 2` -eq 0 ]
then
echo "$a is even"
else
echo "$a is odd"
fi
Input:
Enter the number
53
SUM OF NATURAL NUMBERS
Algorithm:
Step 1: Start
Start 2: Read the number n
Step 3: Initialize I as 0 and Sum as 0
Step 4: As long as the value I is less than n repeat following steps 4 & 5
Step 5: add the value I to the sum & increment I value by 1 each time
Step 6: If the condition fails, then print the value of sum.
Step 7: Stop
Program:
echo enter the number
read n
i=1
sum=0
while [ $i -le $n ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "sum is: $sum"
Input :
Enter the number 4
Output :
Sum is 10
FACTORIAL
Algorithm:
Step 1: Start
Step 2: Read the value n
Step 3: Initialize the variables f and I as 1.
Step 4: As long as the value I is less than or equal to n repeat step 5
Step 5: multiply f with I and increment the value I
Step 6: If the condition is false, then print the factorial value
Program:
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 the factorial is $f
Input:
Enter the number 5
Output:
The factorial is 120
FIBONACCI SERIES
Algorithm:
Step 1: Start
Step 2: Read the value n
Step 3: Initialize a as 0 and b as 1.
Step 4: Print the value of a and b first
Step 5: As long the value n is greater then 2 repeat following steps (a) and (b)
Add a with b and assign the value into c
Assign b to a and c to b
Step 6: Print the value of c
Step 7: Each time decrement the value of n by 1
Program:
echo enter the number
read n
a=0
b=1
echo "the fibonacci series "
echo "$a"
echo "$b"
while [ $n -gt 2 ]
do
c=`expr $a + $b`
a=$b
b=$c
echo "$c"
n=`expr $n - 1`
done
Input:
Enter the number 6
Output:
The fibonacci series
0 1 1 2 3 5
SUM OF DIGITS OF A NUMBER
Algorithm:
Step 1: Start
Step 2: Input the number
Step 3: Test if number not equal to 0 then divide the number by 10 and get the remainder.
Step 4: Add the remainder to the total and rearrange the number.
Step 5: Repeat Step 3 and step 4
Step 6: Print the total value.
Step 7: Stop the program.
Program:
echo "enter the number"
read num
total=0
while test $num –ne 0
do
d=`expr $num % 10`
tot=`expr $tot + $d`
num=`expr $num/10`
done
echo "the sum of digit is $tot"
Input:
Enter the number :
1234
Output
The sum of digit 10
PALINDROME
Algorithm:
Step 1: Start
Step 2: Read the value n
Step 3: Initialize r as 0
Step 4: As long as n value not equal to 0 repeat steps 5,6,7
Step 5: Find the remainder value of n and assign to d (compute d=n%10)
Step 6: Multiply r by 10 and add with d and assign to r (compute r=r*10+d)
Step 7: divide n by 10 (compute n=n/10)
Step 8: Compare r and s
Step 9: If both are equal print the given number is palindrome.
Otherwise it is not palindrome
Step 10: Stop
Program:
echo enter the number
read n
r=0
s=$n
while [ $n -ne 0 ]
do
d=`expr $n % 10`
r=`expr $r \* 10 + $d`
n=`expr $n / 10`
done
if [ $r = $s ]
then
echo "palindrome"
else
echo "not a palindrome"
fi
Input:
Enter the number 121
Output:
Palindrome
REVERSE A NUMBER
Algorithm:-
Step 1: Start the program
Step 2: Input the number ‘num’
Step 3: Initialize count=0
Step 4: Find single digit d in num as num%10, it will give the left most digit
Step 3: construct the reverse number rev as rev*10+d.
Step 4: Reconstruct num=num/10
Step 5: Is num is greater than 0, if yes goto step 4. Otherwise goto next step.
Step 6: Print reverse number.
Step 7: Stop the program.
Program:-
echo program to reverse the digit
echo Enter the number
read num
rev=0
while test $num –ne 0
do
d=`expr $num % 10`
num=`expr $num / 10`
rev=`expr $rev \* 10 + $d`
done
echo The reverse digit is $rev
Input:-
Enter the number
12345
Output:-
The reverse digit is 54321
Amstrong
Algorithm:
Step 1: Start
Step 2: Enter the value n
Step 3: Assign n to b and initialize c to 0 (initialize b=n, c=0)
Step 4: As long as n value greater than 0 repeat the steps 5,6,7,8
Step 5: Find the remainder value of n and assign to r (compute r=n%10)
Step 6: Find the cube value of r and assign to i. (compute i=r*r*r )
Step 7: Divide n by 10 (compute n=n/10)
Step 8: Add the value i to c and assign added value to c (compute c=c+i)
Step 9: Compare b and c
Step 10: If both are equal print the given number is amstrong
Otherwise it an not an amstrong number.
Program:
echo enter the number
read n
b=$n
c=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
i=`expr $r \* $r \* $r`
n=`expr $n / 10`
c=`expr $c + $i`
done
if [ $b = $c ]
then echo "$b is amstrong"
else
echo "$b is not amstrong"
fi
Input:
Enter the number 153
Output:
153 is amstrong
COUNTING NUMBER OF DIGITS IN A NUMBER
Algorithm:
Step 1: Start the program
Step 2: Input the number
Step 3: Initialize count =0
Step 4: If number not equal to 0 then divide the number by 10, then increment the count value by 1 and reconstruct the number
Step 5: Repeat step 4.
Step 6: Print the count value
Step 7: Stop the program
Program:-
echo “Enter a number”
read num
count=0
while test $num –ne 0
do
d=`expr $num % 10`
count=`expr $count + 1`
num=`expr $num / 10`
done
echo “Number of digits are $count”
Input:-
Enter a number
15675
Output:-
Number of digits are 5
FINDING BIGGEST DIGIT OF A NUMBER
Algorithm:-
Step 1: Start the program
Step 2: Input the number
Step 3: Divide the number by 10 and get the remainder.
Step 4: Assign the remainder as big
Step 5: If number>0 then divide the number by 10 and test whether the
remainder is greater than big.
Step 6: If it is big then assign the remainder as big and rearrange the number.
Step 7: Repeat steps 5 and 6
Step 8: Print the biggest digit.
Step 9: Stop the program.
Program:-
echo “Enter a number”
read n
big=`expr $n % 10`
while test $n –ne 0
do
d=`expr $n % 10`
if test $d –gt $big
then
big=$d
fi
n=`expr $n / 10`
done
echo “biggest digit is $big”
Input:-
Enter a number
12589
Output:-
Biggest digit is 9