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.
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.
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");
}
}
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).
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.