Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-12-2012
Registered User
 
Join Date: Apr 2012
Posts: 33
Thanks: 28
Thanked 0 Times in 0 Posts
[Solved] Why code run not correctly

Hi there can anyone help me
here is my code

Code:
echo "Type in a positive number"
read X
I=2
while [ $(( $X > $I )) ]
do
if [ $(( $X % $I == 0)) ]
then
echo "It is not prime"
break
else
if [ $(($X - $I == 1)) ]
then
echo "It is  prime"
break
else
I=$(( $I + 1))
fi
fi
done

idk why when I am entering there 5 it says that it is not prime any ideas why it is working not cirrectly??? Thanks in advanace!!
Sponsored Links
    #2  
Old 07-12-2012
guruprasadpr's Avatar
Shrink...ing
 
Join Date: Jun 2009
Location: India
Posts: 674
Thanks: 30
Thanked 215 Times in 214 Posts
Hi

The closing brackets were placed at the wrong places:



Code:
echo "Type in a positive number"
read X
I=2
while [ $X>$I ]
do
    if [ $(($X%$I)) == 0 ]
    then
    echo "It is not prime"
    break
    else
    if [ $(($X-$I)) == 1 ]
    then
    echo "It is  prime"
    break
    else
    I=$(( $I + 1))
    fi
    fi
done

The Following User Says Thank You to guruprasadpr For This Useful Post:
FUTURE_EINSTEIN (07-12-2012)
Sponsored Links
    #3  
Old 07-12-2012
Registered User
 
Join Date: Apr 2012
Posts: 33
Thanks: 28
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by guruprasadpr View Post
Hi

The closing brackets were placed at the wrong places:



Code:
echo "Type in a positive number"
read X
I=2
while [ $X>$I ]
do
    if [ $(($X%$I)) == 0 ]
    then
    echo "It is not prime"
    break
    else
    if [ $(($X-$I)) == 1 ]
    then
    echo "It is  prime"
    break
    else
    I=$(( $I + 1))
    fi
    fi
done

Thanks for answering I tried to remove all the spaces in arithmetic expressions but still it generates wrong answer
    #4  
Old 07-12-2012
guruprasadpr's Avatar
Shrink...ing
 
Join Date: Jun 2009
Location: India
Posts: 674
Thanks: 30
Thanked 215 Times in 214 Posts
Hi

Its not just about spaces, its about the brackets as mentioned earlier. Try running the corrected code which i pasted, it works fine.

Guru
Sponsored Links
    #5  
Old 07-12-2012
Registered User
 
Join Date: Apr 2012
Posts: 33
Thanks: 28
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by guruprasadpr View Post
Hi

Its not just about spaces, its about the brackets as mentioned earlier. Try running the corrected code which i pasted, it works fine.

Guru

oh, get my mistake
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[Solved] problem - connecting code with external file Telis Shell Programming and Scripting 4 03-15-2012 02:14 PM
[Solved] Running shell code in AppleScript without Terminal ShadowofLight OS X (Apple) 9 06-30-2011 03:52 PM
[solved] merging two files and writing to another file- solved mlpathir Shell Programming and Scripting 1 10-07-2010 06:41 PM
HP-UX will not boot correctly intern UNIX for Dummies Questions & Answers 5 05-24-2006 08:19 AM
why the PATH can not be set correctly? yishen UNIX for Dummies Questions & Answers 5 07-23-2002 10:09 PM



All times are GMT -4. The time now is 08:53 AM.