0% found this document useful (0 votes)
280 views4 pages

Write A Shell Script To Find Sum of Digits of A Number: "Input No: "

This document contains code snippets for several shell scripts: 1. A script to find the sum of digits in a number. 2. A script to reverse a given number. 3. A simple calculator script that allows addition, subtraction, multiplication and division of two numbers and continues until the user chooses to quit. 4. A Fibonacci series generator script that prints the Fibonacci series up to a number entered by the user. 5. A script to check if a given number is prime or not. 6. A script to sort numbers in ascending or descending order after taking numbers from the user as input.

Uploaded by

Manak Soni
Copyright
© Attribution Non-Commercial (BY-NC)
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)
280 views4 pages

Write A Shell Script To Find Sum of Digits of A Number: "Input No: "

This document contains code snippets for several shell scripts: 1. A script to find the sum of digits in a number. 2. A script to reverse a given number. 3. A simple calculator script that allows addition, subtraction, multiplication and division of two numbers and continues until the user chooses to quit. 4. A Fibonacci series generator script that prints the Fibonacci series up to a number entered by the user. 5. A script to check if a given number is prime or not. 6. A script to sort numbers in ascending or descending order after taking numbers from the user as input.

Uploaded by

Manak Soni
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 4

1.

Write a shell script to find sum of digits of a number

echo -n "Input no : " read no length=`expr length $no` while [ $length -ne 0 ] do b=`expr substr $no $length 1` ans=`expr $ans + $b` length=`expr $length - 1` done echo "Sum of Digit is : $ans"

2. Write a shell scrit to reverse a given number


if [ $# -eq 1 ] then if [ $1 -gt 0 ] then num=$1 sumi=0 while [ $num -ne 0 ] do lnum=`expr $num % 10` sumi=`expr $sumi * 10 + $lnum` num=`expr $num \/ 10` done echo "Reverse of digits is $sumi of $1"else echo " Number is less than 0" fi else echo "Insert only one parameter " fi

7. Write a shell script for a simple calculator


Clear sum=0 i="y" echo " Enter one 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=`expr $n1 / $n2` echo "Div = "$sum;; *)echo "Invalid choice";; esac echo "Do u want to continue ?" read i if [ $i != "y" ] then exit fi done

9. Write a shell script to find Fibonacci series


Code: $ vi fibonacci.sh echo Enter n for Fibonacci series: - read n echo Fibonacci series is: - echo 0 echo 1 i=0 j=1 cnt=2 while [ $cnt le $n ] do k=`expr $i + $j` i= $j j= $k echo $k cnt=`expr $cnt + 1` done

12. Write a shell script to check that a given no is prime or not


Solution:
echo "Enter a number: " read num i=2 f=0 while [ $i -le `expr $num / 2` ] do if [ `expr $num % $i` -eq 0 ] then f=1

fi i=`expr $i + 1` done if [ $f -eq 1 ] then echo "The number is composite" else echo "The number is Prime" fi

15. Write a shell script to sort given N Numbers


echo enter the no of element read n1 for (( i=0; i<$n1; i++ )) do echo enter `expr $i + 1` the element. read a[$i] done for (( i=0; i<$n1; i++ )) do for (( j=`expr $i + 1`; j<$n1; j++ )) do if [ ${a[$i]} -gt ${a[$j]} ] then x=${a[$i]} a[$i]=${a[$j]} a[$j]=$x fi done done echo 1.Ascending 2.Descending echo enter your choice... read c if [ $c = 1 ] then echo the ascending order is.... for (( i=0; i<$n1; i++ )) do echo ${a[$i]} done elif [ $c = 2 ] then echo the descending order is... for (( i=$n1; i>0; i-- )) do echo ${a[`expr $i - 1`]} done else

echo wrong choice...... fi

You might also like