PHP Basic
PHP Interview Programs
- PHP Interview Programs
- PHP Abundant Number
- PHP Amicable Number
- PHP Armstrong Number
- PHP Average of N Numbers
- PHP Automorphic Number
- PHP Biggest of three numbers
- PHP Binary to Decimal
- PHP Common Divisors
- PHP Composite Number
- PHP Condense a Number
- PHP Cube Number
- PHP Decimal to Binary
- PHP Decimal to Octal
- PHP Disarium Number
- PHP Even Number
- PHP Evil Number
- PHP Factorial of a Number
- PHP Fibonacci Series
- PHP GCD
- PHP Happy Number
- PHP Harshad Number
- PHP LCM
- PHP Leap Year
- PHP Magic Number
- PHP Matrix Addition
- PHP Matrix Division
- PHP Matrix Multiplication
- PHP Matrix Subtraction
- PHP Matrix Transpose
- PHP Maximum Value of an Array
- PHP Minimum Value of an Array
- PHP Multiplication Table
- PHP Natural Number
- PHP Number Combination
- PHP Odd Number
- PHP Palindrome Number
- PHP Pascalβs Triangle
- PHP Perfect Number
- PHP Perfect Square
- PHP Power of 2
- PHP Power of 3
- PHP Pronic Number
- PHP Prime Factor
- PHP Prime Number
- PHP Smith Number
- PHP Strong Number
- PHP Sum of Array
- PHP Sum of Digits
- PHP Swap Two Numbers
- PHP Triangular Number
PHP Program to Check Magic Number
Photo Credit to CodeToFun
π Introduction
In the world of programming, the concept of magic numbers is a fascinating one.
A magic number is a number in which the eventual sum of its digits, when the sum is recursively calculated, leads to a single-digit number that is 1. If the final sum is not 1, the number is not considered magic.
In this tutorial, we'll delve into a PHP program that checks whether a given number is a magic number or not.
π Example
Let's take a look at the PHP code that checks for the magic number property.
<?php
// Function to check if a number is magic
function isMagicNumber($num)
{
while ($num > 9)
{
$sum = 0;
// Calculate the sum of digits
while ($num > 0)
{
$sum += $num % 10;
$num /= 10;
}
$num = $sum;
}
// Check if the final sum is 1
return ($num == 1);
}
// Driver program
// Replace this value with your desired number
$number = 19;
// Check if the number is magic
if (isMagicNumber($number))
{
echo "$number is a Magic Number.\n";
}
else
{
echo "$number is not a Magic Number.\n";
}
?>
π» Testing the Program
To test the program with different numbers, simply replace the value of $number in the code.
19 is a Magic Number.
Run the script to see whether the number is a magic number or not.
π§ How the Program Works
- The program defines a function isMagicNumber that takes an integer as input and checks if it is a magic number.
- Inside the function, it iteratively calculates the sum of the digits of the number until the number becomes a single-digit number.
- It then checks if the final sum is equal to 1, indicating a magic number.
π Between the Given Range
Let's take a look at the php code that checks for Magic Numbers within the specified range.
<?php
// Function to check if a number is magic
function isMagicNumber($num)
{
while ($num > 9)
{
$sum = 0;
// Sum the squares of each digit
while ($num > 0)
{
$digit = $num % 10;
$sum += $digit * $digit;
$num /= 10;
}
$num = $sum;
}
// Check if the final value is 1
return $num == 1;
}
// Driver program
echo "Magic Numbers in the range 1 to 50:\n";
for ($i = 1;$i <= 50;$i++)
{
if (isMagicNumber($i))
{
echo $i . " ";
}
}
?>
π» Testing the Program
Magic Numbers in the range 1 to 50: 1 10 19 28 37 46
To test the program, run the script. It will output the magic numbers within the specified range.
π§ How the Program Works
- The program defines a function isMagicNumber that checks if a given number is a magic number.
- Inside the function, it uses a while loop to repeatedly sum the squares of the digits until the number becomes a single-digit number.
- The main section iterates through the range from 1 to 50 and prints the magic numbers.
π§ Understanding the Concept of Magic Numbers
A magic number is a number in which the sum of its digits leads to a single-digit number, and that single-digit number is 1.
For example, the number 19 is a magic number because the sum of its digits (1 + 9) is 10, and the sum of 1 and 0 is 1.
π’ Optimizing the Program
While the provided program is effective, consider exploring and implementing optimizations or additional features based on your specific requirements.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (PHP Program to Check Magic Numberο»Ώ), please comment here. I will help you immediately.