adp 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

EX No.

2 A

Use of control structures in Shell Programming

AIM:

To evaluate the use of shell programming control Structures.

DESCRIPTION:

(a) if ..... fi structure:

The if... fi structure allows shell to make decisions and execute

statements conditionally.

SYNTAX: if [expression]

then statements

fi

(b) if ....... else statement:

If else statements can be used to select an option from a given set of

options

SYNTAX: if [expression]

then

(c) while loop:

else

fi

Statement(s)

Statement

The while loop enables us to execute a set of commands repeatedly until

some condition occurs

SYNTAX: while [condition]

do

Statement (5)

done
(d) for loop:

The for loop operates on list of items it repeals a set of commands for
every

item in a list.

SYNTAX: for((int i=o; i<n; i++)

do

Statement(s)

done.

Programs :

a) To find whether a given voter is eligible to vote.

AIM:

To find whether a given voter is eligible to vote

ALGORITHM:

1. Start

2. Declare a variable to enter age

3. And check whether the age is above or equal to 18

4. If it is above 18, then the voter is eligible to vote.

5. If the age is below 18 then the voter is not eligible to vote.

6. End

PROGRAM:

echo Enter your age

read age

echo Your Age is $age

if [ $age -ge 18 ]

then

echo You are eligible for voting.


else

echo You are not eligible for voting.

fi

OUTPUT :

b) To find the grade of a student using multiple if

AIM:

To find the grade of a student using multiple if

ALGORITHM:

1. Start

2. Declare a variable to get mark a student

3. If mark> 90 then grade A, if mark >= 80 then grade B.

4. If mark>=70 then grade C, if mark>=60 then grade b

5. If mark>= 50 then grade E else grade F.

6. End.

PROGRAM:

echo Enter your marks

read mark

if [ $mark -ge 90 ]

then
echo Your Grade is A.

elif [ $mark -ge 80 ]

then

echo Your Grade is B.

elif [ $mark -ge 70 ]

then

echo Your Grade is C.

elif [ $mark -ge 60 ]

then

echo Your Grade is D.

elif [ $mark -ge 50 ]

then

echo Your Grade is E.

else

echo You Failed.

Fi

OUTPUT :

c) To find whether the given year is a leap year or not

AIM:

To find whether the given year is a leap year or not


ALGORITHM:

1. Start

2. Declare a variable to enter year.

3. Divide the year by 4/ remainder

4. If the remainder is equal to o then its leap year.

5. If its not zero then its not a leap year

6. End.

PROGRAM:

echo Enter The Year.

read y

c=$((y%4))

if [ $c -eq 0 ]

then

echo It is a Leap Year.

else

echo It is not a Leap Year.

Fi

OUTPUT :

d) To find the factorial of a given number using white and for loop

AIM:

To find the factorial of a given number using while and for loop

i) Using while loop


ALGORITHM:

1. Start

2. Declare the number to find factorial

3. Read the number, Declare factorial = 1

4. Using while loop, if the number if more than o

5. Then the factorial is found by fact = $(expr $face/$n). n=$(n-1)


expression. I

6. End.

PROGRAM:

echo Enter the number

read num

n=$num

fact=1

while [ $n -gt 0 ]

do

fact=$((fact * n))

n=$((n – 1))

done

echo The factorial of $num is $fact

OUTPUT :

ii) Using for loop:

ALGORITHM:

1. Start

2. Declare the number to find factorial


3. Read the number, Declare factorial

4. using for loop assign i=1; i<=num, itt (in for loop)

5. fact if assigned as $ (expr $ fact 1 + $1)

6. factorial can be found.

7. End.

PROGRAM:

echo Enter th

read num

fact=1

for ((i=1;i<=num;i++))

do

fact=$((fact * i))

done

echo The factorial of sum is $fact

OUTPUT :

e) To find the fibonacci of a given numbers using while loop

AIM:

To find the fibonacci of a given numbers using while loop

ALGORITHM:
1. Start

2. Get a number

3. Use white loop to compute the fibonacci by using the below formula.

4. fibo = $((a+b))

5. Swap a = $b,b= $+n

6. End.

PROGRAM:

echo program for Fibonacci series

echo enter the number

read n

f=1

j=0

echo Fibonacci series of $n is

echo $j

echo $f

for ((i=0; i<=n; i++ ))

do

k=$((j \+ f))

12 j=$f

f=$k

echo $k

done

OUTPUT :
f) To find the greatest of three numbers using if elif...if

AIM:

To find the greatest of s numbers using if elif....if

ALGORITHM:

1. Start

2. Declare 3 numbers.

3. if 'a' is greater than 'b' and 'c', 'a' is larger else if 'b' is more than 'a' and
'c',

'b' is greater else i 'c' is more than 'a' and 'b','c' is greater

6. End.

PROGRAM:

echo Enter 3 Numbers

read a

read b

read c

if test $a -gt $b && test $a -gt $c ]

then

echo a is greatest
elif test $b -gt $c

then

echo b is greatest

else

echo c is greatest

fi

OUTPUT :

RESULT :

Thus the program using control structures in shell programming was


compiled and

executed successfully.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy