≡ Menu

Shell script to read a number and find whether the number is odd or even

{ 10 comments… add one }
  • Sammy Le Jerk May 16, 2014, 5:41 am

    read n
    n2=`expr $n % 2`
    if [ $n2 -eq 0 ];
    then
    echo “even”
    else
    echo “odd”
    fi

  • Saikiran December 3, 2012, 5:29 pm

    echo “Program to check even or odd number”
    echo “Enter a number”
    read n
    a=`expr $n % 2`
    if [ $a -eq 0 ] ; then #Semicolon is most important for Executing if-else
    echo “It is an even number”
    else
    echo “It is an odd number”
    fi

  • nibash October 6, 2012, 9:59 am

    if[ $($n % 2) -eq 0]
    then
    echo “even”
    else
    echo “odd”

    • Sammy Le Jerk May 16, 2014, 5:36 am

      nevermind

  • Myd September 3, 2012, 1:56 pm

    odd() { let ${1: -1}%2 && return 0; return 1; }

  • Jon disnard March 23, 2012, 2:53 am

    is_even() {
    case $(( 10#$1 % 2 )) in
    0) return 0 ;;
    *) return 1 ;;
    esac
    }

  • bhaskar February 1, 2011, 5:23 am

    correct it as:
    if [ $rem -eq ‘0’ ]

  • Kristada673 August 22, 2010, 8:38 am

    @Aakash Alfred: No buddy, that doesn’t work. It displays everything as odd……..

  • Aakash Alfred June 24, 2010, 5:48 am

    # On the third line: Instead of rem=$(( $n % 2 ))
    # You can use ;
    rem= `expr $n % 2`

    • novendra August 10, 2011, 12:29 pm

      $(( $n % 2 ))
      this syntax not work properly in shell script
      error in brackets

Security: Are you a robot or human?

Leave a Comment