Write A Shell Program To Find The Largest Among Three Numbers

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

Write a Shell program to find the largest among three numbers.

PROGRAM echo "Enter the first number:"read aecho "Enter the second number:"read becho "Enter the third number:"read cif [ $a -gt $b -a $a -gt $c ]thenecho "$s is greater"elif [ $b -gt $c ]thenecho "$b is greater"elseecho "$c is greater"fi OUTPUT -bash-3.2$ sh larthree.shEnter the first number:20Enter the second number:30Enter the third number:1030 is greater-bash-3.2$ 26. Write a Shell program to find the largest among n different numbers.PROGRAM echo "Enter the number of elements:"read nl=0for((i = 1 ; i <= n ; i++))doecho "Enter the number:"read noif [ $no -gt $l ]thenl=$nofidoneecho "The largest numbers is : $l"

OUTPUT -bash-3.2$ sh largest.shEnter the number of elements:5Enter the number:44Enter the number:55Enter the number:33Enter the number:22Enter the number:11The largest numbers is : 55

27. Write a Shell program to find the largest digit of a number.PROGRAM echo "Enter a number:"read ns=0while [ $n -gt 0 ]dor=`expr $n % 10`if [ $r -gt $s ]thens=$rfin=`expr $n / 10`doneecho "The largest digit is : $s" OUTPUT -bash-3.2$ sh large.shEnter a number:143The largest digit is : 4-bash-3.2$ sh large.shEnter a number:786The largest digit is : 8

Write a Shell program to find the sum of n different numbers.PROGRAM echo "Enter the number of elements:"read ns=0for((i = 1 ; i <= n ; i++))doecho "Enter the number:"read nos=`expr $s + $no`doneecho "The sum is : $s" OUTPUT -bash-3.2$ sh sum.shEnter the number of elements:5Enter the number:11Enter the number:22Enter the number:33Enter the number:44Enter the number:55The sum is : 165

Write a Shell program to find the sum of digits of a number.PROGRAM echo "Enter a number:"read ns=0while [ $n -gt 0 ]dor=`expr $n % 10`s=`expr $s + $r`n=`expr $n / 10`doneecho "The sum of digit is : $s"

-bash-3.2$ sh sumdigit.shEnter a number:14The sum of digit is : 5-bash-3.2$ sh sumdigit.shEnter a number:1983The sum of digit is : 21

30. Write a Shell program to print the reverse of a number.PROGRAM echo "Enter a number:"read nt=$ns=0while [ $n -gt 0 ]dor=`expr $n % 10`s=`expr $r + $s \* 10`n=`expr $n / 10`doneecho "The reverse of the number $t is $s" OUTPUT -bash-3.2$ sh revnum.shEnter a number:123The reverse of the number 123 is 321

31. Write a Shell program to find the factorial of a number using for loop.PROGRAM echo "Enter a number:"read nf=1for((i = 1 ; i <= n ; i++))dof=`expr $f \* $i`doneecho "The factorial of $n is $f"

-bash-3.2$ sh factorial.shEnter a number:5The factorial of 5 is 120

32. Write a Shell program to generate Fibonacci series.PROGRAM echo "Enter the number of terms:"read necho "Fibonacci series is:"a=-1b=1c=0for((i = 1 ; i <= n ; i++))doc=`expr $a + $b`echo $ca=$bb=$cdone OUTPUT -bash-3.2$ sh fibonacci.shEnter the number of terms:5Fibonacci series is:01123

You might also like