Print 1...100 using php code with the following instructions

Instructions

1.Print the numbers 1…100
2.For multiples of 3, print “Mpasho” instead of the number
3.For multiples of 5, print “Star” instead of the number
4.For multiples of 3 and 5, print “MpashoStar” instead of the number
5.Print it out as html/css so if we run it on a web server, it’ll work.

So how far have you got?

i just started with trying this code which don’t seem to even work for starters

<?php
header('Content-Length: 81');
ob_end_clean();
ob_implicit_flush(true);
foreach(range(1, 100) as $num){
printf("%d \ ", $num);
sleep(1);
}

Okay. Nice start. However, the first 3 commands are not needed, and I would use a for loop rather than foreach.

1 Like

I’d say you can lose the first three lines (after the php intro, of course) for now, they’re just adding complexity that isn’t required. For example, if you think about the header you’re sending in the first proper line, what makes you think that the total content of the page will only be 81 characters long, even though you’re going to display 100 numbers? Even if they were all only single-digit numbers, that would total more than 81 characters, so that’s obviously not right. Forget about specifying content length, you won’t need to do that for the exercise.

The steps you list in the first post are a good sequence to get this working, so first get it to print the numbers 1 to 100. Hint: look at the for() statement. Then start adding bits for the extended behaviour. I’d also get rid of the sleep() statement as that’s not going to help, and you need a php closing line as well.

ETA - as Gandalf typed while I was editing.

1 Like

A remainder operator in PHP is %

So $num % 3 will tell you whether the number is a multiply of 3 or not

1 Like

Wonder if question has any relation to this? https://www.ihub.co.ke/jobs/view/3348/interns

3 Likes

Sounded like homework to me. But that’s about the same…

Yes it does just got a look at it.I am still a student not really interested in that intern spot but our lecture dared us with the question.I can handle it well with java but my PHP is somehow not strong.Here is the solution using java.

#include <stdio.h>
int main()
{
int i;

for ( i = 1; i <= 100; i++ )
{
   if (i%3==0)
   {
      printf("Mpasho");
   }
   if (i%5==0)
   {
      printf("Star");
   }
   if(i%3 && i%5)
   {
      printf("%d",i);
   }
   printf("\n");           
}
}

The logic isn’t quite right (test it and the results won’t quite match the requirements (especially #5). Your pseudo-logic should be

  • create an unordered list (will need css to hide the bullet points)
    • Create a loop (i) from 1 to 100
      • create a li element
      • If i is divisible by 3, add Mpasho to the li element text
      • If i is divisible by 5, add Star to the li element text
      • ONLY if i is NOT divisible by either 3 OR 5 is the value of i added to the li element text

Don’t normally like answering homework questions but what the heck. Haven’t tried the below but it should work :slight_smile:

<?php

$html = array_reduce(range(1, 100), function ($html, $number) {
    if (!($number % 3) && !($number % 5)) {
        $result = 'MpashoStar';
    } else if (!($number % 3)) {
        $result = 'Mpasho';
    } else if (!($number % 5)) {
        $result = 'Star';
    } else {
        $result = $number;
    }
    return $html."<li>$result</li>";
}, '');


echo <<< EOF_HTML
  <!doctype html>
  <html>
    <head>
      <meta charset="utf-8">
      <title>Example</title>
      <style>
        ul {
          list-style-type: none;
        }      
      </style>
    </head>
    <body>
        <ul>$html</ul>
    </body>
  </html>
EOF_HTML;
1 Like

Try searching the internet for FizzBuzz and you should find thousands of alternatives (whoever set the question just substituted MpashoStar for FizzBuzz to make it less obvious that it is a very common test question).

test questions may vary, but some are very common :wink:

2 Likes
<!DOCTYPE html>
<html>
<head>
  <title> 1-100</title>
</head>
<body>
<?php 
$html = array_reduce(range(1,100), function ($html, $x)
{
if (($x%3==0)&&($x%5==0))
{ 
       $result = "MpashoStar<br/>";
}	
elseif ($x%3==0)
	{ 
        $result = "Mpasho<br/>";
}
elseif($x%5==0)
{ 
        $result = "Star<br/>";
}
else 
{
       $result = $x;
}
       echo "$result";
}
);
?>

</body>
</html>

Welcome to the forums, @Cyrusdon1.

Perhaps you could explain the differences between your code and that already given, and say why you prefer your version?

1 Like

There are plenty of sites around that explain why using ($x%3==0)&&($x%5==0) is the wrong way to approach this problem. The simple explanation is that adding a third condition expands the number of tests from three to seven and adding a fourth condition expands it to fifteen - where you only really need as many tests as there are conditions when you write the answer properly.

Here’s an example (untested but should give the general idea of how the code should look) with only one test per condition instead of testing all the combinations of conditions as well.

$h = array_reduce(range(1, 100), function ($h, $n) {
$t='';
if(!($n%3)) $t.='"Mpasho';
if(!($n%5)) $t.='Star';
return $h.(strlen($t)>0?$t:$n).'<br>';
},'');

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.