shamuraq Posted May 15, 2009 Share Posted May 15, 2009 Hi guys, I manage to use '%' to differentiate odd or even numbers. Problem is how do i count no. of odd or even numbers between say, 2408 and 2531? Thanx in advance... Quote Link to comment https://forums.phpfreaks.com/topic/158302-solved-even-number-display-count/ Share on other sites More sharing options...
Cosizzle Posted May 15, 2009 Share Posted May 15, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/158302-solved-even-number-display-count/#findComment-834937 Share on other sites More sharing options...
Ken2k7 Posted May 15, 2009 Share Posted May 15, 2009 $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; } Quote Link to comment https://forums.phpfreaks.com/topic/158302-solved-even-number-display-count/#findComment-834949 Share on other sites More sharing options...
shamuraq Posted May 15, 2009 Author Share Posted May 15, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/158302-solved-even-number-display-count/#findComment-834981 Share on other sites More sharing options...
Ken2k7 Posted May 15, 2009 Share Posted May 15, 2009 Try mine. Quote Link to comment https://forums.phpfreaks.com/topic/158302-solved-even-number-display-count/#findComment-834982 Share on other sites More sharing options...
shamuraq Posted May 15, 2009 Author Share Posted May 15, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/158302-solved-even-number-display-count/#findComment-834983 Share on other sites More sharing options...
Ken2k7 Posted May 15, 2009 Share Posted May 15, 2009 Just change $start and $end to whatever random numbers you want. Don't worry about the rest. If you want to understand it, I'll be happy to explain it, but all you need to do is change those 2 values and the rest will take care of the rest. Quote Link to comment https://forums.phpfreaks.com/topic/158302-solved-even-number-display-count/#findComment-834984 Share on other sites More sharing options...
shamuraq Posted May 15, 2009 Author Share Posted May 15, 2009 i think i see the rationality behind your script Ken... I'll give it a try... Quote Link to comment https://forums.phpfreaks.com/topic/158302-solved-even-number-display-count/#findComment-834988 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.