How to check Armstrong number
An Armstrong number is a number that is the sum of the cubes of its digits is equal to the number itself.
Armstrong Number Logic:
xyz= (x*x*x) + (y*y*y) + (z*z*z)
Example: 0, 1, 153, 371, 407, 471, etc.
<?php
$inputNumber=22;
$numSum = 0;
$checkNumber = $inputNumber;
while($checkNumber>0)
{
$rem = $checkNumber % 10;
$numSum = $numSum + $rem*$rem*$rem;
$checkNumber = $checkNumber / 10;
}
if ($inputNumber == $numSum) {
echo "$inputNumber is an Armstrong Number.";
}else{
echo "$inputNumber is not an Armstrong Number.";
}
?>
22 is not an Armstrong Number.
<?php
function checkArmstrong($inputNumber=""){
$numSum = 0;
$checkNumber = $inputNumber;
while($checkNumber>0)
{
$rem = $checkNumber % 10;
$numSum = $numSum + $rem*$rem*$rem;
$checkNumber = $checkNumber / 10;
}
if ($inputNumber == $numSum) {
return true;
}else{
return false;
}
}
$inputNumber=371;
if (checkArmstrong($inputNumber)) {
echo "$inputNumber is an Armstrong Number.";
}else{
echo "$inputNumber is not an Armstrong Number.";
}
?>
371 is an Armstrong Number.
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning