06.06.2013 Views

Computer Programming with GNU Smalltalk - Free

Computer Programming with GNU Smalltalk - Free

Computer Programming with GNU Smalltalk - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

96 <strong>Computer</strong> <strong>Programming</strong> <strong>with</strong> <strong>GNU</strong> <strong>Smalltalk</strong><br />

5.<br />

6.<br />

/ \<br />

/ \<br />

/ \<br />

/ \<br />

\ /<br />

\ /<br />

\ /<br />

\ /<br />

\/<br />

"answer_4_5.st"<br />

| theNumber isPrime i |<br />

Transcript show: 'Please enter a number: '.<br />

theNumber := stdin nextLine asInteger.<br />

i := 2.<br />

[theNumber \\ i = 0] whileFalse: [<br />

i := i + 1.<br />

].<br />

(i = theNumber) ifTrue: [<br />

Transcript show: theNumber printString, ' is a prime number.'; cr.<br />

] ifFalse: [<br />

Transcript show: theNumber printString, ' is not a prime number. It is<br />

devidable by ', i printString, '.'; cr.<br />

].<br />

Please enter a number: 3<br />

3 is a prime number.<br />

"answer_4_6.st"<br />

99 to: 1 by: -1 do: [ :x |<br />

Transcript show: x printString, ' bottles of beer on the wall, ', x<br />

printString, ' bottles of beer.'; cr.<br />

(x > 1) ifTrue: [<br />

Transcript show: 'Take one down and pass it around, ', (x - 1)<br />

printString, ' bottles of beer on the wall.'; cr.<br />

] ifFalse: [<br />

Transcript show: 'Take one down and pass it around, no more bottles<br />

of beer on the wall.'; cr.<br />

].

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!