Jump to content

Recommended Posts

Hmm I think this should work.

 

$start_num = 2408;
$end_num = 2531;
$total = 0;
$even = 0;
$odd = 0;
while($start_num < $end_num) {
$start_num++;
if ($start_num%2 == 0) {
	$even ++;
}
else if($start_num%2 == 1) {
	$odd ++;
}
$total++;
}

echo 'total: '.$total;
echo '<br>';
echo 'even: '.$even;
echo '<br>';
echo 'odd: '.$odd;

 

returns

 

total: 123
even: 61
odd: 62

$start = 2408;
$end = 2531;

$num = $end - $start + 1;
$isEven = $num % 2 == 0;
$end_even = $num % 2 == 0;

if ($isEven) {
     $e = $num / 2;
     echo 'Total odd numbers between ' . $start . ' and ' . $end . ' is: ' . $e . '<br />Total even numbers between ' . $start . ' and ' . $end . ' is: ' . $e;
} else {
     $e = round($num / 2);
     echo 'Total odd numbers between ' . $start . ' and ' . $end . ' is: ';
     echo $end_even? $e : $e - 1;
     echo '<br />Total even numbers between ' . $start . ' and ' . $end . ' is: ';
     echo $end_even? $e - 1 : $e;
}

Hi Cosizzle...

i used your codes and inserted into my script; scary. my localhost took sometyme to respond and only part of the script echo out. Script goes like this:

 

<?

$a = rand(1000,9999);

$b= $a + rand(13,49);

$total = 0;

$even = 0;

 

echo "How many even numbers are there between $a and $b?";

 

while($a<$b || $a%2 ==0){

$even++;

}

$f1 = $even + rand(0,10);

$f2 = $even + rand(0,11);

$f3 = $even + rand(0,12);

?>

    <table width="36%" border="0" cellpadding="0" cellspacing="3">

      <tr>

        <td><? echo "A) $even";?></td>

        <td><? echo "B) $f1";?></td>

      </tr>

      <tr>

        <td><? echo "C) $f2";?></td>

        <td><? echo "D) $f3";?></td>

      </tr>

    </table>

    </td>

Try mine. :)

 

I'm trying to understand urs Ken...

Problem is both the values are randomised so there's no way to tell if the starting integer is odd or even. Whereas urs started by taking it as even. How do i manipulate your script to cater to randomised value?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.