GTU OS Program - 5

5. Write a shell script which will accept a number and display first n prime numbers as output.

Complete code of the program

read -p "Enter the value of n (or range) " n
echo "The prime numbers are: "
m=2
while [ $m -le $n ]
do
        i=2
        flag=0
        while [ $i -le `expr $m / 2` ]
        do
                if [ `expr $m % $i` -eq 0 ]
                then
                        flag=1
                        break
                fi
                i=`expr $i + 1`
        done
        if [ $flag -eq 0 ]
        then
                echo $m
        fi
        m=`expr $m + 1`
done

Interpreting the prime.sh file and executing it in terminal

tkanu025@hp:~/lab_solutions$ chmod +x prime.sh
tkanu025@hp:~/lab_solutions$ ./prime.sh
Output
Gujarat Technological university OS programs

gtu os lab manual,

os GTU Practical,

operating system Programs,

Operating system shellscript programs,

Gtu study material 3140705,

OS 3140702,

os practical list,

shell script programming,

gtu os practical,

os lab solution gtu

Comments

YouTube

Popular posts from this blog

GTU OOP Programs - 11

Mini project ideas for Operating System

GTU OS Program - 8