PHP Program to Check whether a number is happy number or not


February 20, 2022, Learn eTutorial
1070

How to check whether a number is a happy number or not?

To check whether a number is a happy number or not first we have to find the square of every digit in the number then we have to add those together and again find the square of every digit of the num number and continue this process until the number becomes 1 or the number continuously gives 4. For example, if the number is 82 then

82 + 22 = 68

62 + 82 = 100

12 + 02 + 02 = 1

So we can conclude that the number 82 is a Happy number.

How to check whether a number is a happy number or not using PHP?

In this program, we check whether a number is a happy number or not. For that first, we are accepting the number from the user and assigning the value to the variable num and after that, we assign the value of the variable num to the variable n. Then we perform the while loop until the condition 'n != 1' and 'n != 4' becomes false and in the block of the loop we assign the return value of the user-defined function checkNum(n). And in the checkNum() function we first assign the value 0 into the variables rem and sum. After that, we perform the while loop until the condition 'num > 0' becomes false and in the block of the loop, we assign the computed values of 'num % 10' into the variable rem, 'sum + (rem *rem)' into the variable sum and 'num / 10' into the variable num and after the completion of the loop we return the value of the variable sum. After completion of all the loops, we check the condition 'n == 1' if true Print the value of the variable num as a happy number otherwise check the condition 'n == 4' if true Print the value of the variable num is not a happy number.

ALGORITHM

Step 1: Accept the number from the user and assign it to the variable num

Step 2: Assign the value of the variable num to the variable n

Step 3: Perform the sub-step until the condition 'n != 1' and 'n != 4' becomes false

        (i) Assign the return value of the user-defined function checkNum(n)

Step 4: Check the condition 'n == 1' if true Print the value of the variable num as a happy number

Step 5: Check the condition 'n == 4' if true Print the value of the variable num is not a happy number

ALGORITHM function: checkNum(num)

Step 1: Assign the value 0 into the variables rem and sum

Step 2: Perform the sub-steps until the condition 'num > 0' becomes false

        (i) Assign the computed value of 'num % 10' into the variable rem

        (ii) Assign the computed value of 'sum + (rem *rem)' into the variable sum

        (iii) Assign the computed integer value of 'num / 10' into the variable num

Step 3: Return the value of the variable sum

PHP Source Code

                                          <?php
function checkNum($num)
{
    $rem = 0;
    $sum = 0;
    while ($num > 0) {
        $rem = $num % 10;
        $sum = $sum + ($rem * $rem);
        $num = intval($num / 10);
    }
    return $sum;
}
$num = readline("Enter the number: ");
$n = $num;
while ($n != 1 && $n != 4) {
    $n = checkNum($n);
}
if ($n == 1)
    print($num . " is a happy number");
else if ($n == 4)
    print($num . " is not a happy number");
?>
                                      

OUTPUT

Example 1
Enter the number: 13
13 is a happy number

Example 2
Enter the number: 15
15 is not a happy number