Tuesday, January 24, 2023

Unix Shell Programming Example for B.Sc, B.Tech, BCA, MCA

 3. Leap year checking.

PROGRAM:

echo "enter the year:"
read n
if [ `expr $n % 4` -eq 0 ]
then
    if [ `expr $n % 100` -eq 0 ]
    then
        if [ `expr $n % 400` -eq 0 ]
        then
            echo "$n is a leapyear"   #2000
        else
            echo "$n is not a leapyear"  #1900
        fi
    else
        echo "$n is a leapyear"  #1996
    fi
else
    echo "$n is not a leapyear"  #1997
fi

OUTPUT:

[bgc@localhost ~]$ sh pc2.sh
enter the year:
1996
1996 is a leapyear
[bgc@localhost ~]$ sh pc2.sh
enter the year:
2000
2000 is a leapyear
[bgc@localhost ~]$ sh pc2.sh
enter the year:
1997
1997 is not a leapyear
[bgc@localhost ~]$ sh pc2.sh
enter the year:
1900
1900 is not a leapyear
[bgc@localhost ~]$

No comments:

Post a Comment