Armstrong Number in PHP


Q. Write a PHP program to check Armstrong Number

<?php
// Define the number
$originalNumber = 153;
$number = $originalNumber;
$sum = 0;
$digits = strlen((string)$number);

// Calculate the sum of the power of digits
while ($number > 0) {
    $digit = $number % 10;
    $sum += pow($digit, $digits);
    $number = (int)($number / 10);
}

// Check if it's an Armstrong number
if ($sum == $originalNumber) {
    echo "$originalNumber is an Armstrong number.";
} else {
    echo "$originalNumber is NOT an Armstrong number.";
}
?> 

Q. Write an algorithm to check Armstrong Number in PHP

1. Start
2. Define an integer originalNumber
3. Set number = originalNumber and sum = 0
4. Count the number of digits and store in digits
5. Repeat while number > 0:
    a. digit = number % 10
    b. sum = sum + pow(digit, digits)
    c. number = number / 10
6. If sum == originalNumber, print "Armstrong number"
7. Else, print "Not an Armstrong number"
8. End



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.