Armstrong Number program in PHP with form and with database
Armstrong Number program in PHP with form and with database
In this tutorial, we will learn about the followings;
- Armstrong Number program in PHP
- Armstrong Number program in PHP with form
- Armstrong Number program in PHP with database
Armstrong Number program in PHP
In this example, programmer stores the values in variables. However, if you interested to see the program about taking values in the form, please see example 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php $num=153; $sum=0; $temp=$num; while($temp!=0) { $rem=$temp%10; $sum=$sum+$rem*$rem*$rem; $temp=$temp/10; } if($num==$sum) { echo "Armstrong number"; } else { echo "not an armstrong number"; } ?> |
Armstrong Number program in PHP with form
In this example, we will take the inputs from the user and store them in variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php if (isset($_POST['submit'])) { $num=$_POST['number1']; $sum=0; $temp=$num; while($temp!=0) { $rem=$temp%10; $sum=$sum+$rem*$rem*$rem; $temp=$temp/10; } if($num==
$sum) { echo "Armstrong number"; } else { echo "not an armstrong number"; } } //end of if ?> |
Armstrong Number program in PHP with database
In this example, we will take the inputs from the user and temporarily store them in variables and then finally stores them permanently in the database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php $conn= mysqli_connect('localhost','root','','arm'); if (isset($_POST['submit'])) { $num=$_POST['number1']; $sum=0; $temp=$num; $sql="INSERT INTO arm(number1,number2)VALUES('$num','$sum')"; $run=mysqli_query($conn,$sql); while($temp!=0) { $rem=$temp%10; $sum=$sum+$rem*$rem*$rem; $temp=$temp/10; } if($num==$sum) { echo "Armstrong number"; } else { echo "not an armstrong number"; } } //end of if ?> |
Database