Results 1 to 6 of 6

Thread: Shell program for the prime number

  1. #1
    Join Date
    Nov 2009
    Posts
    56

    Shell program for the prime number

    Hello Guys,

    I have to write the shell one program. The program code should check if the given number is prime or not. I know the java programming, but it's different than Shell programming.
    Please provide me shell programming code for the prime number. Your help will be greatly appreciated.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Shell program for the prime number

    Hi , below is the shell programming code of the prime number. Please review that carefully:

    echo "Enter number"
    read a
    q=2
    p=$a

    until [ $q == $a ]
    do

    if [ `expr $a % $J` == 0]
    echo "Given number is not prime number"
    exit

    else
    q=`expr $q+ 1`

    done
    echo "Given number is prime number"

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Shell program for the prime number

    Hi friend,
    You need implement the below code logic into your shell programming. Use simple if loop concept for this. I have used "cnt" variable in the below program code:
    int count = 0
    for x = 1 to num,// "num" is the number for which we have to check the prime number

    if num % x = 0
    cnt=cnt+1

    if cnt <=2, then
    print => Prime Number

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Shell program for the prime number

    Did you try below shell programming code for prime number? If not then try it on your system. The program will let you know if number is prime number or not prime number.
    l=2
    rm=1

    echo -e "provide a number: \c"
    read nm

    while [ $i -le `expr $nm / 2` -a $rem -ne 0 ]; do
    rm=`expr $nm % $i`
    l=`expr $l + 1`
    done

    if [ $rem -ne 0 ]; then
    echo -e "$provided number is prime\n"

    if [ $nm -lt 2 ]; then
    echo -e "$provided number is not prime\n"

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Shell program for the prime number

    Hello,
    In your program you need to use "if [ `expression $z % $x` == 0]" this conditional statement. In this statement the "z" is the user defined number. While the "x" is the number which varies from zero to the given number. At the time of the execution, this conditional statement check if the given number is divided by any number. If it is divided by any number then it must be prime number.

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Shell program for the prime number

    Hi,
    You should include the nested for loop in you prime shell program. Use two "for loops". First is for the incrementation of the 0 - number and another for loop to check the prime number condition.
    for ( a=$Numr+1; a <= $Mn-1; a++ )

    for (( b=2; b <= $a-1; b++ ))

    if [ `$a % $b` = 0 ]

    echo" Prime number"

Similar Threads

  1. Problem with C# program which calculates the prime number
    By $Bird$ in forum Software Development
    Replies: 4
    Last Post: 29-11-2010, 12:54 PM
  2. Shell program to reverse a number
    By Bottlenecked in forum Software Development
    Replies: 5
    Last Post: 12-01-2010, 02:20 PM
  3. program to print prime factor of given number
    By teena_pansare in forum Software Development
    Replies: 4
    Last Post: 27-11-2009, 10:21 AM
  4. program to check prime number in java
    By Bansi_WADIA in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 11:02 PM
  5. Prime number
    By mad4jack in forum Software Development
    Replies: 3
    Last Post: 03-10-2008, 09:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,534,334.03553 seconds with 17 queries