English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Print numbers from 1 to 100, but print TIC for the multiples of 3 and print TAC for multiples of 5 and TOE for multiples of 3 and 5....

All the best.

2007-03-15 02:59:32 · 4 answers · asked by NeevarP M 3 in Computers & Internet Programming & Design

4 answers

For i = 1 To 100
If (i Mod 3 = 0 And i Mod 5 = 0) Then
Response.Write("Toe")
ElseIf i Mod 3 = 0 Then
Response.Write("Tic")
ElseIf i Mod 5 = 0 Then
Response.Write("Tac")
Else
Response.Write(i.ToString())
End If
Response.Write(" ")
Next i

2007-03-15 05:17:28 · answer #1 · answered by Libran 2 · 0 0

ok, For this we use the modulus operator % , so if you want to know if a number is multiple of 5, just put:

if ((number % 5) == 0)
printf(TAC);

The same thing with the others.

To generate the numbers just make a loop like this:

for (number = 1; number <= 100; number++)

Then mix everything up:

for (number = 1; number <= 100; number++)
{

if ((number % 5) == 0)
printf(TAC);

....
}

2007-03-15 03:32:29 · answer #2 · answered by Rafael Mateo 4 · 0 0

Fun question, and that Perl code was short 'n sweet. Here a couple of mine, in PHP:

A short one:

for($i=0; $i<100; print(++$i%3? ($i%5? $i : 'TAC') : ($i%5? 'TIC' : 'TOE'))."
");

A recursive one:

function rTicTacToe($start, $end) {
return $start>$end? '' : ($start%3? ($start%5? $start : 'TAC') : ($start%5? 'TIC' : 'TOE'))."
" . rTicTacToe(++$start, $end);
}
echo rTicTacToe(1, 100);

One using while & switch:

$i=0;
$r='';
while ($i++<100) {
switch ($i) {
case ($i%15==0):
$r.="TOE";
break;
case ($i%5==0):
$r.="TAC";
break;
case ($i%3==0):
$r.="TIC";
break;
default:
$r.=$i;
}
$r.="
";
}
echo $r;

A geeky one:

$x = $z = 814090528;
$n = 0;
$s = 'TACTICTOE';
while ($n < 100) {
echo ($n++ && ($x&3)? substr($s, (($x&3) - 1) * 3, 3) : $n) . "
";
$x = (int) (($x? $x : $z) / 4);
}

An encoded one, one line:

eval( gzuncompress( base64_decode(
'eJxLyy/SUMm0NbBWybQxNDCwLijK
zCvR0NZWyVQ1tgfKqJraq2RaqYc4O
qtrWkH46iGezupAIX9XdU1NPSWbpC
I7JU1rAL7zEgk='
)));

And finally, an OO one:

class TicTacToe {

var $modulators;

function TicTacToe() {
if ($args = func_get_args())
foreach ($args as $key => $arg)
if (is_a($arg, 'modulator'))
$this->modulators[$key] = &$args[$key];
if ($this->modulators)
$this->sort();
}

function compare($a, $b) {
return $a->number == $b->number ? 0 : $a->number < $b->number ? 1 : -1;
}

function sort() {
usort($this->modulators, array(get_class($this), 'compare'));
}

function output($start, $end) {
$result = '';
for ($i = $start; $i <= $end; $i++) {
for ($j = 0; $j < count($this->modulators); $j++) {
$temp = false;
if ($temp = $this->modulators[$j]->output($i)) {
$result .= "$temp
";
break;
}
}
if (!$temp)
$result .= "$i
";
}
return $result;
}
}

class modulator {

var $number;
var $text;

function modulator($number, $text) {
$this->number = $number;
$this->text = $text;
}

function output($n) {
return $n % $this->number? false : $this->text;
}
}

$TicTac = new TicTacToe(
new modulator( 3, 'TIC'),
new modulator( 5, 'TAC'),
new modulator(15, 'TOE')
);

echo $TicTac->output(1, 100);

Oops, long post, LOL

2007-03-15 15:36:42 · answer #3 · answered by GodBuster 5 · 0 0

It gets cut off if it is on one line, for some reason, so remove the new lines.

perl -e 'print$_%5?
$_%3?$_:"TIC"
:$_%3?"TAC":
"TOE","\n"for(1..100)'

2007-03-15 03:03:54 · answer #4 · answered by David D 7 · 1 0

fedest.com, questions and answers