Alternate Primes till N | Function for checking number is prime or not ; if flag = 0 then number is prime and return 1 otherwise return 0 ; Function for printing alternate prime number ; counter is initialize with 0 ; looping through 2 to n - 1 ; function calling along with if condition ; if counter is multiple of 2 then only print prime number ; Driver code ; Function calling
< ? php function prime ( $ num ) { $ flag = 0 ; for ( $ i = 2 ; $ i <= $ num / 2 ; $ i ++ ) { if ( $ num % $ i == 0 ) { $ flag = 1 ; break ; } } if ( $ flag == 0 ) return 1 ; else return 0 ; } function print_alternate_prime ( $ n ) { $ counter = 0 ; for ( $ num = 2 ; $ num < $ n ; $ num ++ ) { if ( prime ( $ num ) == 1 ) { if ( $ counter % 2 == 0 ) echo $ num . " " ; $ counter += 1 ; } } } $ n = 15 ; echo " Following ▁ are ▁ the ▁ alternate ▁ prime ▁ " . " number ▁ smaller ▁ than ▁ or ▁ equal ▁ to ▁ " . $ n . " STRNEWLINE " ; print_alternate_prime ( $ n ) ; ? >