Showing posts with label sum of digits in php. Show all posts
Showing posts with label sum of digits in php. Show all posts

Saturday, May 25, 2024

PHP using sum of digits

Q. Sum of Digits in php 



 Example :-

<?php
$number =1222;
$sum=0;
$rem=0;
for($i=0;$i<=strlen($number);$i++)
{
    $rem = $number% 10;
    $sum = $sum + $rem;
    $num = $number/10;
}
echo " number is $number = Addition of digits
<br> Answer is $sum";


?>


Sum of Digits in php 

Output:-

sum of digits php program



Q. Sum of digit using form  php

Example :-

<form method="post">
    Enter number
    <input type="number" name="number"><br><br>
    <input type="submit" value="submit">
</form>
<?php
if( $_POST)
{


$number =$_POST['number'];
$sum=0;
$rem=0;
for($i=0; $i<=strlen($number); $i++)
{
    $rem = $number% 10;
    $sum = $sum + $rem;
    $number = $number/10;
}
echo " number is $number Addition of digits
<br> Answer is $sum";
}

?>


Sum of digit using form  php 

Output :-
sum of digits php program

Related Articles:-

Armstrong number program php

factorial number program in php

palindrome number

how to run php code in xampp

php even odd program

create login form php