Back to the 2023 paper

Module III: Basics of Web Programming

20237m

Design a simple HTML form to input a string and to display whether it is Armstrong or not within the form submitted. Use a PHP script.

Worked SolutionAI Assisted

Armstrong number checker (HTML form + PHP)

An Armstrong number equals the sum of its digits each raised to the power of the number of digits (e.g. 153 = 1³ + 5³ + 3³; 9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴).

armstrong.php (form and result on the same page)

<!DOCTYPE html>
<html>
<head><title>Armstrong Number Check</title></head>
<body>
  <h2>Armstrong Number Checker</h2>
  <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
    <label>Enter a number:
      <input type="text" name="num" required>
    </label>
    <input type="submit" value="Check">
  </form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    input=trim(input = trim(_POST["num"]);

    if (!ctype_digit($input)) {
        echo "<p>Please enter digits only.</p>";
    } else {
        n=strlen(n      = strlen(input);   // number of digits
        $sum    = 0;
        for (i=0;i = 0; i < n;n; i++) {
            sum+=pow((int)sum += pow((int)input[i],i], n);
        }

        if (sum==(int)sum == (int)input) {
            echo "<p><b>$input</b> is an Armstrong number.</p>";
        } else {
            echo "<p><b>$input</b> is NOT an Armstrong number.</p>";
        }
    }
}
?>
</body>
</html>

How it works:

  1. The form posts the value to the same script (PHP_SELF).
  2. PHP reads it from $_POST['num'] and validates that it contains only digits.
  3. It counts the digits, sums each digit raised to that power, and compares the sum with the original number.
  4. The result is printed below the form, so the answer appears within the submitted page.

Sample: input 153 → 1 + 125 + 27 = 153 → Armstrong. Input 123 → 1 + 8 + 27 = 36 → not Armstrong.

Similar questions

AI LearningWeb Platform

Coursify — Personal AI Course Generator

Struggling with a difficult subject or concept? Turn any engineering topic into a structured course with interactive lessons, quizzes, and research-backed explanations.