Updated March 27, 2023
Introduction to Armstrong Number in PHP
Armstrong number is a type of number whose value/number is equal to the sum of the cubes of each digits. Those types of numbers are called as Armstrong Numbers. Some of the Armstrong numbers are 0, 1, 153, 371, 407, 471, etc.
The logic behind Armstrong number:
- First, you have to take the number as input to check whether it is Armstrong or not.
- Store that number in a variable.
- Now take that variable for sum.
- Now divide that number with 10 value until the quotient is 0.
- Cube the remainder values.
- Compare the sum’s variable and also the number variable value (If both numbers same then it is Armstrong Number).
Examples to Check Armstrong Number in PHP
Below are the examples using various methods such as: for, while, do-while.
Example #1: Using For Loop in PHP
This program is to check whether the number is Armstrong number or not using For Loop. In the below PHP program, the input number is stored in the armnum2 variable and also assigning 0 to the total3 variable. Now the new variable “x3” is assigned in the For loop using initialization, incrementation, and condition inside the For loop by assigning Armnum2 variable to x3 as the starting number as initialization, Condition as x3!=0 to get out from the loop, incrementation by dividing x3 with 10 and storing in x3 value.
Rem3 variable is to get the remainder value. Now the cubing of remainder value inside the For loop to get all the remainder values using the initialization, incrementation and condition values of For loop because as the logic, input number and the cubes of the digits of the number should be equal to confirm as an Armstrong Number.
Code:
<?php
$armnum2=407;
$total3=0;
for($x3=$armnum2;$x3!=0;$x3=$x3/10)
{
$rem3=$x3%10;
$total3=$total3+$rem3*$rem3*$rem3;
}
if($armnum2==$total3)
{
echo "Yes, Number $armnum2 is an Armstrong number";
}
else
{
echo "No, Number $armnum2 it is not an armstrong number";
}
?>
Output:
Example #2: Using HTML Form and For Loop program
Here the form basic concept is to include the user input with the help of For Loop. Users can enter whatever input value he wants to enter with the help of the visible prompt in the browser after running the PHP form for loop script. Check using the below code and know.
This is the For Loop program with the HTML Form using the Post method to get the direct user input from the user. Form method has post with the input parameters as the number and submit is used, Number is to transfer the input number to the program to check whether the number/variable value is Armstrong number or not. After that same Loop program like above continues to check the Armstrong number. Likewise for all the programs.
Code:
<html>
<body>
<form method="post">
Enter Armstrong Number/Other:
<input type="number" name="number3">
<input type="submit" value="Submit3">
</form>
</body>
</html>
<?php
if($_POST)
{
//It is to get the number1 value entered
$number3 = $_POST['number3'];
$sum3 = 0;
//Loop with the condition of quotient =0
for($a3 = $number3;$a3!=0;$a3=$a3/10)
{
$rem3 = $a3 % 10; //finds the reminder
$sum3 = $sum3 + ( $rem3 * $rem3 * $rem3 ); //sum by cubing the reminder values and stored in other variable
}
//if and else to check whether it is an armstrong number or not
if( $number3 == $sum3 )
{
echo "Yes $number3 an Armstrong Number";
}else
{
echo "$number3 is not an Armstrong Number";
}
}
?>
Output:
Example #3: Using While Loop in PHP
This is the While Loop program to check whether the number is Armstrong number or not. To come out of the loop condition is included inside of the While loop as x1 not equals to 0. Rem1 variable is assigned to get the remainder values. By using the remainder values and its cubes until the condition x1 is equaled to 0. And then x1 is the input number is divided by 10 and stored in x1 variable to get all the remainder values using the While loop. The same thing works with the Do While loop program.
Code:
<?php
$armnum=407;
$total1=0;
$x1=$armnum;
while($x1!=0)
{
$rem1=$x1%10;
$total1=$total1+$rem1*$rem1*$rem1;
$x1=$x1/10;
}
if($armnum==$total1)
{
echo "Yes, Number $armnum is an Armstrong number";
}
else
{
echo "No, Number $armnum it is not an armstrong number";
}
?>
Output:
Example #4: Using HTML Form and While Loop program
Here the form basic concept is to include the user input. User can enter whatever input value he wants to enter. Check the below code and know.
Code:
<html>
<body>
<form method="post">
Enter Armstrong Number/Other:
<input type="number" name="number1">
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
if($_POST)
{
//It is to get the number1 value entered
$number1 = $_POST['number1'];
//Now storing the entered number in number1 variable
$a1 = $number1;
$sum1 = 0;
//Loop with the condition of quotient =0
while( $a1 != 0 )
{
$rem1 = $a1 % 10; //finds the reminder
$sum1 = $sum1 + ( $rem1 * $rem1 * $rem1 ); //sum by cubing the reminder values and stored in other variable
$a1 = $a1 / 10; //finding quotient. if 0 loop continues
}
//if and else to check whether it is an armstrong number or not
if( $number1 == $sum1 )
{
echo "Yes $number1 an Armstrong Number";
}else
{
echo "$number1 is not an Armstrong Number";
}
}
?>
Output:
Example #5: Using DO-While Loop in PHP
Code:
<?php
$armnum1=407;
$total2=0;
$x2=$armnum1;
do
{
$rem2=$x2%10;
$total2=$total2+$rem2*$rem2*$rem2;
$x2=$x2/10;
}
while($x2!=0);
if($armnum1==$total2)
{
echo "Yes, Number $armnum1 is an Armstrong number";
}
else
{
echo "No, Number $armnum1 it is not an armstrong number";
}
?>
Output:
Example #6: Using HTML Form and Do While loop
Here the form basic concept is to include the user input. User can enter whatever input value he wants to enter.
Code:
<html>
<body>
<form method="post">
Enter Armstrong Number/Other:
<input type="number" name="number2">
<input type="submit" value="Submit2">
</form>
</body>
</html>
<?php
if($_POST)
{
//It is to get the number1 value entered
$number2 = $_POST['number2'];
//Now storing the entered number in number1 variable
$a2 = $number2;
$sum2 = 0;
//Loop with the condition of quotient =0
do
{
$rem2 = $a2 % 10; //finds the reminder
$sum2 = $sum2 + ( $rem2 * $rem2 * $rem2 ); //sum by cubing the reminder values and stored in other variable
$a2 = $a2 / 10; //finding quotient. if 0 loop continues
}while( $a2 != 0 );
//if and else to check whether it is an armstrong number or not
if( $number2 == $sum2 )
{
echo "Yes $number2 an Armstrong Number";
}else
{
echo "$number2 is not an Armstrong Number";
}
}
?>
Output:
Recommended Articles
This is a guide to Armstrong Number in PHP. Here we discuss meaning, logic behind Armstrong number, examples along with code implementation and output. You may also have a look at the following articles to learn more –