Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

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;

  1. Armstrong Number program in PHP 
  2. Armstrong Number program in PHP with form 
  3. 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.

<?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.

<?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.

<?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

Armstrong Number program in PHP with form and with database

Figure: Armstrong Number program in PHP with form and with database

Fazal Rehman Shamil Click Here to Know More
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325
Dear Professors and Resarchers!You are welome to Cite these tutorials in your research or slides etc. Please don't forget to mention the reference of website. Copy Paste of text is strcitly forbidden. Images can be reuse because images are protected with watermark.

Leave a Reply

Your email address will not be published. Required fields are marked *