Hi can someone please indicate where is the error in the php code i wanted to sort of translate from java to php pleaase help:


This is the Java Code:

public class Break{
public static void main(String[] args){
int i,j;
System.out.println("Prime numbers between 1 to 50 : ");
for (i = 1;i < 150;i++ ){
for (j = 2;j < i;j++ ){
if(i % j == 0)
{
break;
}
}
if(i == j)
{
System.out.print(" " + i);
}
}
}
}

And here is my attempt to make a php equivalent please point out my mistake:
<?php
$i;
$j;

for($i=1; $i<150; $i++){
for($j=2; $j<$i; $j++){
if($i%$j == 0) {break;}
if($i == $j){echo ' '.$i."
";}}
}

?>

In your php code, the if block that echos the numbers is inside the second for-loop while it needs to be outside.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.