Arithmatic operations, factorial of a number, while loop, prime number, etc
EX.NO:4.aARITHMETIC
OPERATIONS USING CASE
Aim:
To write a shell program to perform the arithmetic operations using
case
Algorithm :
Step 1 : Read the input variables and assign the value
Step 2 : Print the various arithmetic operations which we are going to
perform
Step 3 : Using the case operator assign the various functions for the
arithmetic
operators.
Step 4 : Check the values for all the corresponding operations.
Step 5 : Print the result and stop the execution.
.
Program code: echo 1.Addition echo 2.Subraction echo 3.Multiplication echo 4.Division echo Enter your choice read a echo Enter the value of b read b echo Enter the value of c read c echo b is $b c is $c case $a in 1)d=`expr $b + $c` echo the sum is $d ;; 2)d=`expr $b - $c` echo the difference is $d ;; 3)d=`expr $b \* $c` echo the product is $d ;; 4)d=`expr $b / $c` echo the quotient is $d ;; esac |
Sample I/P : 1.Addition 2.Subraction 3.Multiplication Division Enter your choice:1 Enter the value of b:3 Enter the value of c:4 b is 3 c is 4 the sum is 7 Sample O/P: b is 3 c is 4 the sum is 7 |
RESULT:Thus the program
was executed and verified successfully
VIVA-VOCE
QUESTIONS:
1. What is case statement in Linux?
2. What does esac
do in Linux?
EX.NO:4.bARITHMETIC
OPERATIONS USING CASE
Aim:
To write a shell program to list of file, list of process ,list of
user login using case.
Algorithm :
Step 1 : Read the input variables and assign the value
Step 2 : Print the
various list of file ,process and user log in which we are going to perform
Step 3 :Using the case operator assign the various linux command for list of file,process and
user login.
Step 4 : Print the result and stop the execution.
Program Code:
clear
echo 1. list of
file
echo 2. list of
process
echo 3. list of
user login
readval
case $val in
1)ls
echo " list
of files are"
;;
2)ps
echo "list of
process are"
;;
3)who
echo " list
of users are"
;;
esac
Sample O/P:
1. list of file
2. list of process
3. list of user
login
Result :
Thus the shell
program to perform arithmetic operations using case is executed and output is
verified successfully
VIVA-VOCE
QUESTIONS:
1. What is case statement in Linux?
2. What does esac
do in Linux?
EX.NO:4.cFACTORIAL OF A NUMBER
Aim:
To write a shell program to find the factorial
of a number.
Algorithm:
step 1. Start
step 2. Read the number x
step 3. [Initialize]
i=1, fact=1
step 4. Repeat step 4 through 6 until i=x
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
Program Code:
clear
echo " enter
any no"
read x
f=1
for((i=1;i<=x;i++))
do
f=`expr $f \* $i`
done
echo "factorial
of $x is $f"
Sample O/P:
Enter any no
5
factorial of $x is
120
RESULT:Thus the program
was executed and verified successfully
VIVA-VOCE QUESTIONS:
1. What is a
factorial and what is its purpose?
2. What
is the use of “expr” in Linux?.
3. What are the symbols for multiplication with expr in shell script?
EX.NO:4.d USING WHILE LOOP AND UNTIL LOOP
Aim:
To write a shell program to print from 1 to
10 using while loop and then using until loop.
Algorithm :
step 1. Start
step 2. [Initialize] The variable i is initialized with value 0 and then it has
been tested for the condition.
step3: If the condition
returns true then the statements inside the body of while loop are executed
else control comes out of the loop.
step4.: value of count is
incremented using ++ operator then it has been tested again for the loop
condition.
Step5:
Print i
Step6:Stop
Using while loop: Program Code: clear i=0 while [ $i -le
10 ] do i=`expr $i + 1` echo
"$i" done Sample O/P: 1 2 3 4 5 6 7 8 9 10 |
Using until loop: Program Code: clear val=1 until [ $val -gt
10 ] do echo $val let val=val+1 done esc done Sample O/P: 1 2 3 4 5 6 7 8 9
10 |
RESULT:
Thus the program was executed and verified
successfully
VIVA-VOCE QUESTIONS:
1. What is loop?
2. What are different types of loop?
3. What is entry control loop?
4. Write syntax of while-loop.
5.
What is event control loop
EX.NO:4.ePRIME NUMBER
Aim:
To write a shell program whether a number
prime or not.
Algorithm :
Step 1:START
Step 2:Take integer variable A
Step 3 :Divide the variable A with (A-1 to 2)
Step 4: If A is divisible by any
value (A-1 to 2) it is not prime
Step 5 :Else it is prime
Step 5 : STOP
Program Code:
clear
echo " Enter
the no"
read a
c=0
for((i=2;i<a;i++))
do
let n=a%i
if [ $n -eq 0 ]
then
c=1
fi
done
if [ $c -eq 1 ]
then
echo "not a
prime"
else
echo "
prime"
fi
Sample I/P :
Enter the no: 53
Sample O/P:
prime
Result :
Thus the shell
program to find the Prime number is executed and output is verified
successfully.
VIVA-VOCE QUESTIONS:
1. What is prime
number?
2. Write
syntax of for-loop.
3. Can Prime Numbers be Negative?
4. What is “–eq” in shell script?
EX.NO:4.fFIBONACCI SERIES
Aim:
To write a shell progrm to generate Fibonacci series.
Algorithm :
Step 1 :Initialise
a to 0 and b to 1.
Step 2 : Print the
values of 'a' and 'b'.
Step 3 : Add the
values of 'a' and 'b'. Store the added value in variable 'c'.
Step 4 : Print the
value of 'c'.
Step 5 :Initialise
'a' to 'b' and 'b' to 'c'.
Step 6 : Repeat
the steps 3,4,5 till the value of 'a' is less than 10.
Program code:
echo enter the
number
read n
a=-1
b=1
i=0
while [ $i –le $n ]
do
t=`expr $a + $b`
echo $t
a=$b
b=$t
i=`expr $i + 1
done
Sample I/P :
Enter the no: 5
Sample O/P:
0
1
1
2
3
5
Result :
Thus the shell program to find the Fibonacci
series is executed and output is verified successfully.
VIVA-VOCE QUESTIONS:
1. What is
Fibonacci series?
2. Why is
Fibonacci important??
3. What are the real life applications of
Fibonacci series?
4.What
is "-le" in shell script?
EX.NO:4.gARMSTRONG NUMBER
Aim:
To write a shell program whether a check
Armstrong number.
Algorithm :
Step 1:
Start
Step 2:
read number
Step 3:
set sum=0 and duplicate=number
Step 4:
reminder=number%10
Step 5:
sum=sum+(reminder*reminder*reminder)
Step 6:
number=number/10
Step 7:
repeat steps 4 to 6 until number > 0
Step 8:
if sum = duplicate
Step 9:
display number is Armstrong
Step 10:
else
Step 11:
display number is not armstrong
Step 12:
stop
Step 13:
Program
Code: echo “Enter a number: “ read a x=$a sum=0 r=0 n=0 While[ $x –gt 0 ] do r=`expr
$x % 10` n= `expr
$r \* $r \* $r` sum= `expr $sum + $n` x=`expr
$x / 10` done if[ $sum –eq $a ] then echo “It ia an Armstrong No” else echo “It ia not an Armstrong No” fi |
Sample O/P: Enter a number: 153 It ia an Armstrong No |
Result :
Thus the shell
program to find the Armstrong No
is executed and output is verified successfully
VIVA-VOCE QUESTIONS:
1. What is meant by Armstrong number?
2. Is 5 an Armstrong number?
3. Is 0 an Armstrong number?
4. Write syntax of while-loop.
5. Write syntax of if function.
EX.NO:4.hNATURAL NUMBER
Comments
Post a Comment