Tuesday, January 24, 2023

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

 8.Print twin prime numbers within a range.

PROGRAM:

echo "enter a range:"
read a
read b
n=$a
while [ $n -le $b ]
do
    c=0
    d=1
    while [ $d -le $n ]
    do
        if [ `expr $n \% $d` -eq 0 ]
        then
            c=`expr $c + 1`
        fi
        d=`expr $d + 1`
    done

    if [ $c -eq 2 ]
    then
        f=$n
        e=`expr $n + 2`
        m=$e
        c=0
        d=1
        while [ $d -le $m ]
        do
            if [ `expr $m \% $d` -eq 0 ]
            then
                c=`expr $c + 1`
            fi
            d=`expr $d + 1`
        done
        if [ $c -eq 2 ]
        then
            echo "twin prime pair=$f,$e"
        fi
       
    fi
n=`expr $n + 1`
done

OUTPUT:

enter a range:
2
10
twin prime pair=3,5
twin prime pair=5,7
[bgc@localhost ~]$ sh 9.sh
enter a range:
3
100
twin prime pair=3,5
twin prime pair=5,7
twin prime pair=11,13
twin prime pair=17,19
twin prime pair=29,31
twin prime pair=41,43
twin prime pair=59,61
twin prime pair=71,73
[bgc@localhost ~]$

No comments:

Post a Comment