0% found this document useful (0 votes)
14 views12 pages

Adp 2b

Uploaded by

md21arshad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

Adp 2b

Uploaded by

md21arshad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Ex No.

2 B

Use of CASE statements and Menu Driven Program

AIM:

To illustrate the use of switch statement in Menu driven using Shell


Programming

ALGORITHM:

1. Start

2. Read the choice from the user.

3. Use switch case statement to do the operation.

3.1 If choice is 1 perform Fibonacci series.

3.2 If choice is 2 write a shell program to check whether a given number is


odd or even.

3.3 If choice is 3, write a shell program to check whether a given year is


Leap year or not.

3.4 If choice is 4 write a shell program to find the greatest of three


numbers.

3.5 If choice is 5, write a shell program to find the sum of the digits of a
given number.

2. Menu Driven program for file operations:

1. Start

2. Using while loop, perform

3.Get the choice from user

4.Create switch case to perform

4.1 If choice is 1, perform cat command

4.2 If choice is 1, perform cp command

4.3 If choice is 3, perform mv command

4.4 If choice is 4, perform wc command

4.5 If choice is 5, perform grep command


4.6 If choice is 6, perform head command

4.7 If choice is 7, perform tail command

4.8 If choice is 8, perform sort command

PROGRAM:

echo "MENU"

echo "1. Fibonacci"

echo "2. Odd or Even"

echo "3. Leap Year"

echo "4. Greatest of Three Numbers"

echo "5. Sum of Digits"

echo "Enter your choice:"

read choice

case $choice in

1)

echo "Program for Fibonacci series"

echo "Enter the number of terms:"

read n

f=1

j=0

echo "Fibonacci series for $n terms is:"

echo "$j"

echo "$f"

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

do

k=$((j + f))

j=$f

f=$k

echo "$k"
done

;;

2)

echo "Program for checking if a number is odd or even"

echo "Enter the number:"

read n

if (( n % 2 == 0 )); then

echo "The given number $n is even"

else

echo "The given number $n is odd"

fi

;;

3)

echo "Program to check if a year is a leap year"

echo "Enter a year:"

read year

if (( year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) )); then

echo "The year $year is a leap year"

else

echo "The year $year is not a leap year"

fi

;;

4)

echo "Enter three numbers:"

read a b c

if [ $a -gt $b ] && [ $a -gt $c ]; then

echo "The greatest number is $a"

elif [ $b -gt $a ] && [ $b -gt $c ]; then

echo "The greatest number is $b"


else

echo "The greatest number is $c"

fi

;;

5)

echo "Program to find the sum of digits of a number"

echo "Enter a number:"

read num

sum=0

temp=$num

while [ $temp -gt 0 ]; do

digit=$((temp % 10))

sum=$((sum + digit))

temp=$((temp / 10))

done

echo "The sum of the digits of $num is $sum"

;;

*)

echo "Invalid choice"

;;

esac

while true; do

echo "MENU"

echo "1. cat"

echo "2. cp"

echo "3. mv"

echo "4. wc"

echo "5. grep -option"


echo "6. head -n"

echo "7. tail -n"

echo "8. sort -option"

echo "Enter your choice:"

read choice

case $choice in

1)

echo "Enter the file name:"

read file

cat "$file"

;;

2)

echo "Enter source file name:"

read s

echo "Enter destination file name:"

read d

cp "$s" "$d"

;;

3)

echo "Enter source file name:"

read s

echo "Enter destination file name:"

read d

mv "$s" "$d"

;;

4)

echo "Enter the file name:"

read file

wc "$file"
;;

5)

echo "Enter the file name:"

read file

echo "Enter the option (v, n, or c):"

read option

echo "Enter the word:"

read word

grep -$option "$word" "$file"

;;

6)

echo "Enter the file name:"

read file

echo "Enter number of lines to display:"

read n

head -$n "$file"

;;

7)

echo "Enter the file name:"

read file

echo "Enter number of lines to display:"

read n

tail -$n "$file"

;;

8)

echo "Enter the file name:"

read file

echo "Enter the sort option (e.g., -r for reverse):"

read option
sort $option "$file"

;;

*)

echo "Please enter a valid choice"

;;

esac

done

OUTPUT : MENU DRIVEN PROGRAMMING

1. FIBONACCI :

2. ODD OR EVEN NUMBER :


3. LEAP YEAR :

4. GREATEST OF THREE NUMBERS :


5. SUM OF DIGITS :

OUTPUT :
1. CAT :

2. CP :

3. MV :

4. WC :
5. GREP :

6. HEAD :

7. TAIL :

8. SORT:
RESULT:

The following menu driven program was compiled and executed


successfully.

You might also like