Sum Of Two Digits in PHP


Q. Write a PHP program to find the sum of two digits

<?php
// Define a two-digit number
$number = 47;

// Check if it's a valid two-digit number
if ($number >= 10 && $number <= 99) {
    $firstDigit = (int)($number / 10);  // Get the tens digit
    $secondDigit = $number % 10;        // Get the ones digit
    $sum = $firstDigit + $secondDigit;

    echo "The sum of digits of $number is: $sum";
} else {
    echo "Please enter a valid two-digit number.";
}
?> 

Q. Write an algorithm to find the sum of two digits in PHP

1. Start
2. Define a two-digit integer number
3. Check if number is between 10 and 99
4. If not, display "Invalid input"
5. Extract the first digit: firstDigit = number / 10
6. Extract the second digit: secondDigit = number % 10
7. Compute sum = firstDigit + secondDigit
8. Print the result
9. 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.