Custom Search

Friday, 20 April 2012

Write a Linux shell script to generate prime number from 1 to n, where n is any positive integer number entered by user, Unix program for prime numbers from 1 to n


Write a Linux shell script to generate prime number from 1 to n, where n is any positive integer number entered by user, Unix program for prime numbers from 1 to n


Program


clear
echo "Enter the number upto which you want prime numbers:"
read n


for ((i=1;i<=n;++))
do
 flag=0
 for ((j=2;j<i;j++))
 do


  if [ 'expr $i % $j' -eq 0]
  then
      flag=1
  fi
  done
  
  if [$flag-eq 0]
  then
    echo $i
  fi


  done


Output:
Enter the number upto which you want prime numbers:
10


1
2
3
5
7
<<< Previous  ||  Current  ||  Next >>>  

9 comments:

  1. i am not getting output for this program please check it is urgent

    ReplyDelete
  2. I think After the if statement there is space

    ReplyDelete
  3. Thanks sir it's perfectly working

    ReplyDelete
  4. read -p "Enter the number : " n
    for ((i=1;i<=n;i++))
    do
    c=0
    for ((j=2;j<i;j++))
    do
    if [[ $((i % j)) -eq 0 ]]
    then
    c=1
    fi
    done
    if [[ $c -eq 0 ]]
    then
    echo $i
    fi
    done
    ~

    ReplyDelete
  5. its working


    read -p "Enter the number : " n
    for ((i=1;i<=n;i++))
    do
    c=0
    for ((j=2;j<i;j++))
    do
    if [[ $((i % j)) -eq 0 ]]
    then
    c=1
    fi
    done
    if [[ $c -eq 0 ]]
    then
    echo $i
    fi
    done
    ~

    ReplyDelete

Laptops