13 April 2013

Shell Script to find Greatest of four numbers using If-Else-If Statement

3 comments

Q: How do I write Bash Script to find Greatest of Four Numbers using If-Else-If Statement


Ans:

#!/bin/bash

read -p "Enter first Number:" n1
read -p "Enter second Number:" n2
read -p "Enter third Number:" n3
read -p "Enter fourth Number:" n4

if((n1>n2)) ; then
 if((n1>n3)); then
   if((n1>n4)); then
     echo "$n1 is a Greatest Number"
   else
     echo "$n4 is a Greatest Number"
   fi
  elif((n3>n4)); then
     echo "$n3 is a Greatest Number"
  else
     echo "$n4 is a Greatest Number"
  fi
elif((n2>n3)); then
  if((n2>n4)); then
     echo "$n2 is a Greatest Number"
  else
     echo "$n4 is a Greatest Number"
  fi
  elif((n3>n4)); then
     echo "$n3 is a Greatest Number"
  else
     echo "$n4 is a Greatest Number"
fi

Output


find greatest of four number using if-else-if statement in shell script

Enjoy :)


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

3 comments:

  1. This is really good but if all numbers are equal then what will happen?

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I don't think that I have an appropriate answer to that but I believe you can get help from the experts who provided me with a Corn Genetics Report Example which was very helpful to me.

    ReplyDelete