PHP Program to Check for Lucky Numbers Last Updated : 27 May, 2024 Comments Improve Suggest changes Like Article Like Report We have given an input number, and our task is to check if it is a lucky number or not in PHP based on the criteria that all digits in the number must be different. Examples: Input: n = 7Output: 7 is a lucky numberInput: n = 9Output: 9 is not a lucky numberBelow are the approaches to check for lucky numbers in PHP: Table of Content Using for LoopUsing ArrayUsing for LoopIn this approach, we are using a for loop to iterate through numbers from 2 to n−1 to check if n has any divisors other than 1 and itself. If any such divisor is found, the count variable is incremented. Finally, if the count is greater than 0, the number n is deemed not lucky; otherwise, it is considered a lucky number. Example: This example shows the implementation of the above-explained approach. PHP <?php $n = 9; $count = 0; for ($i = 2; $i < $n; $i++) { if ($n % $i == 0) { $count++; } } if ($count > 0) { echo "$n is not a lucky number"; } else { echo "$n is a lucky number"; } ?> Output9 is not a lucky numberUsing ArrayIn this approach, we are using an array to track removed elements, initializing it with false values representing numbers that are not yet removed. The code iteratively marks multiples of each prime number as removed, finally determining if the input number is lucky based on its presence in the array. Example: This example shows the implementation of the above-explained approach. PHP <?php $n = 9; $removed = array_fill(0, $n + 1, false); $i = 2; while ($i <= $n) { if (!$removed[$i]) { $j = $i + $i; while ($j <= $n) { $removed[$j] = true; $j += $i; } } $i++; } if ($removed[$n]) { echo "$n is not a lucky number"; } else { echo "$n is a lucky number"; } ?> Output9 is not a lucky number Comment More infoAdvertise with us Next Article PHP Program to Check for Lucky Numbers G gpancomputer Follow Improve Article Tags : PHP Similar Reads PHP | Check if a number is prime A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The smallest prime number is 2, which is the only even prime number. All other even numbers are not prime because they are divisible by 2.2 is prime because it can only be divided by 1 and 2.3 is 3 min read How to extract Numbers From a String in PHP ? Extracting numbers from a string involves identifying and isolating numerical values embedded within a text. This process can be done using programming techniques, such as regular expressions, to filter out and retrieve only the digits from the string, ignoring all other characters.Here we have some 3 min read Output of PHP programs | Set 3 Predict the output of below PHP programs: Question 1 PHP <?php $number = array(0, 1, one, two, three, 5); $num = preg_grep("/[0-5]/", $number); print_r($num); ?> Options: Array([0]=>0 [1]=>1 [2]=>one [3]=>two [4]=>three [5]=>5) Array([2]=>one [3]=>two [4]=> 3 min read Write a program to display Reverse of any number in PHP ? Write a program to reverse the digits of an integer. Examples : Input : num = 12345 Output: 54321 Input : num = 876 Output: 678 It can be achieved using Iterative way or Recursive Way Iterative Method: Algorithm: Input: num (1) Initialize rev_num = 0 (2) Loop while num > 0 (a) Multiply rev_num by 2 min read How to format Phone Numbers in PHP ? In this article, we will learn how to format phone numbers using PHP. When we need to store a phone number then we store the formatting phone number. Using PHP, we can easily format phone numbers. Approach: In this article, we will format the phone number using the preg_match() method. We can use th 1 min read Like