How To Implement PHP Code For Checking Armstrong number

admin_img Posted By Bajarangi soft , Posted On 27-11-2020

An Armstrong number is the one whose value is equal to the sum of the cubes of its digits. example 0, 1, 153, 371, 407, 471, etc are Armstrong numbers.So today we implement php code to calculate it .

How To Implement PHP Code For Checking Armstrong number

Complete Code For Checking Armstrong number.

<!DOCTYPE html>
<html>
<head>
    <title>How To Implement PHP Code For Checking Armstrong number</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<style>
    body {
        background: black;
    }
</style>
<body>
<body>
<div class="container">
    <br/><br/>
    <div class="text-center">
        <h1 id="color" style="color: white;">PHP Code For Checking Armstrong number</h1>
    </div>

    <div class="well">
        <form action="" method="post" enctype="multipart/form-data">
            <label>Enter a Number</label>
            <input type="text" name="number" value=""><br><br>
            <input type="submit" value="submit" class="btn btn-success" name="submit">
        </form>
        <?php
        if (isset($_POST['submit'])) {

            //get the number entered
            $number = $_POST['number'];
            //store entered number in a variable
            $a = $number;
            $sum = 0;
            //run loop till the quotient is 0
            while ($a != 0) {
                $rem = $a % 10; //it will find reminder
                $sum = $sum + ($rem * $rem * $rem); //cube the reminder and add it to the sum variable till the loop ends
                $a = $a / 10; //find quotient. if 0 then loop again
            }
            //if the entered number and $sum value matches then it is an armstrong number
            if ($number == $sum) {
                echo "$number an Armstrong Number";
            } else {
                echo "$number is not an Armstrong Number";
            }
        }
        ?>
    </div>
</div>
</body>
</html>

Related Post