0% found this document useful (0 votes)
42 views

Factorial: "Enter A Number: "

1. The document contains 8 code snippets demonstrating various programming concepts in shell scripting like conditionals, loops, functions, string operations etc. 2. The snippets include programs to check if a number is even/odd, calculate factorials, check if a number is Armstrong, check primality, compare/operate on strings, and create a menu driven calculator. 3. The last snippet is a shell script to print a student marksheet based on total marks and calculate percentage and grade.

Uploaded by

Jack Rockets
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Factorial: "Enter A Number: "

1. The document contains 8 code snippets demonstrating various programming concepts in shell scripting like conditionals, loops, functions, string operations etc. 2. The snippets include programs to check if a number is even/odd, calculate factorials, check if a number is Armstrong, check primality, compare/operate on strings, and create a menu driven calculator. 3. The last snippet is a shell script to print a student marksheet based on total marks and calculate percentage and grade.

Uploaded by

Jack Rockets
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Even/Odd
read -p "Enter a number: " number
if [ $((number%2)) -eq 0 ]
then
  echo "Number is even."
else
  echo "Number is odd."
fi

2. Factorial

echo -n "Enter a number: "


read number
factorial=1
for(( i=1; i<=number; i++ ))
do
  factorial=$[ $factorial * $i ]
done
echo "The factorial of $number is $factorial"

3. armstrong

echo "Enter a number: "


read c
x=$c
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 $c ]
then
echo "It is an Armstrong Number."
else
echo "It is not an Armstrong Number."
fi
4. Prime or not

num=29
for((i=2; i<=num/2; i++))
do
  if [ $((num%i)) -eq 0 ]
  then
    echo "$num is not a prime number."
    exit
  fi
done
echo "$num is a prime number."

5. To find lowest of two numbers

Echo enter two numbers


Read n1 n2
If[$n1 – lt $n2]
then
echo “first no is smallest.”
else
echo “second no is smallest.’
fi

…………………………………………..
Greatest of two nos:

echo "Enter Num1"


read num1
echo "Enter Num2"
read num2

if [ $num1 -gt $num2 ]


then
echo $num1
else
echo $num2
fi

6. Code for Shell script to perform operations like compare string, concatenate,
find length, occurrence of word in a string and reverse a string in Unix / Linux /
Ubuntu

clear
i="y"
a=0
t=0
while [ $i = "y" ]

do
clear
echo "1.Compare 2 strings :"
echo "2.Concatanet string"
echo "3.Find length of string"
echo "4.Occurance of word"
echo "5.Reverse of string"
echo "6.Exit"
echo "Enter your choice"
read ch
case $ch in
1)echo "Enter first String"
read s1
echo "Enter second string "
read s2
if [ $s1 = $s2 ]
then
echo "Two strings are equal "else
echo "Two strings are not equal"
fi;;
2)echo "Enter one string "
read s1
echo "Enter second string "
read s2
echo $s1 $s2;;
3)echo "Enter any String"
read s1
t=`echo $s1|wc -c`
t=`expr $t - 1`
echo "Length of "$s1" is "$t;;
4)echo "Enter any String "
read s1
echo "Enter word u want to find occurance of:"
read c1
t=`echo $s1|wc -c`
t=`expr $t - 1`
echo "length "$t
while [ $t -ne 0 ]
do
temp=`echo $s1|cut -c $t`
temp2=`echo $temp2 $temp`
#echo $temp2
if [ $temp2 = $c1 ]
then
a=`expr $a + 1`
t=`expr $t - 1`
else
t=`expr $t - 1`
fi
done
echo "Occurance of "$c1" is "$a;;
5)echo "Enter any string :"
read s1
t=`echo $s1|wc -c`
t=`expr $t - 1`
echo "length "$t
while [ $t -ne 0 ]
do
temp=`echo $s1|cut -c $t`
echo $temp
temp2=`echo $temp2 $temp`
t=`expr $t - 1`
done
echo $temp2;;
6)exit;;
*)echo "Invalid choice";;
esac
echo "Do u want to continue ?"
read i
if [ $i != "y" ]
then
exit
fi
done

7. A shell script to make a menu driven calculator using case

#!/bin/bash

clear

sum=0

i="y"

echo " Enter first  no."

read n1

echo "Enter second no."

read n2

while [ $i = "y" ]

do

echo "1.Addition"

echo "2.Subtraction"

echo "3.Multiplication"

echo "4.Division"

echo "Enter your choice"

read ch

case $ch in

    1)sum=`expr $n1 + $n2`


     echo "Sum ="$sum;;

        2)sum=`expr $n1 - $n2`

     echo "Sub = "$sum;;

    3)sum=`expr $n1 \* $n2`

     echo "Mul = "$sum;;

    4)sum=`echo "scale=2;$n1/$n2"|bc`

        echo "div=" $sum;;

    *)echo "Invalid choice";;

esac

echo "Do u want to continue ?[y/n]"

read i

if [ $i != "y" ]

then

    exit

fi    

done   

8. shell script to print a students marksheet


echo "Enter the five subject marks for the student"
read m1 m2 m3 m4 m5
sum1=`expr $m1 + $m2 + $m3 + $m4 + $m5`
echo "Sum of 5 subjects are: " $sum1
per=`expr $sum1 / 5`
echo " Percentage: " $per
if [ $per -ge 60 ]
then
echo "You get Distinction”
elif [ $per -ge 50 ]
then
echo “You get First class”
elif [ $per -ge 40 ]
then
echo "You get Second class"
else
echo "You get Fail"
fi

You might also like