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

  1. read -p "Enter the value of n (or range) " n
  2. echo "The prime numbers are: "
  3. m=2
  4. while [ $m -le $n ]
  5. do
  6. i=2
  7. flag=0
  8. while [ $i -le `expr $m / 2` ]
  9. do
  10. if [ `expr $m % $i` -eq 0 ]
  11. then
  12. flag=1
  13. break
  14. fi
  15. i=`expr $i + 1`
  16. done
  17. if [ $flag -eq 0 ]
  18. then
  19. echo $m
  20. fi
  21. m=`expr $m + 1`
  22. done

Interpreting the prime.sh file and executing it in terminal

  1. tkanu025@hp:~/lab_solutions$ chmod +x prime.sh
  2. 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 OS Program - 11 Filters

GTU OOP Program - 17 tic-tac-toe

GTU OS Program - 13 Date Validator