EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Armstrong Number in PHP

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » PHP Tutorial » Armstrong Number in PHP

Armstrong Number in PHP

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • 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:

armstrong 3

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.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,063 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

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:

armstrong 2

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:

Armstrong Number in PHP 3

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:

armstrong 4

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:

Armstrong Number in PHP 5

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:

Armstrong Number in PHP 6

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 –

  1. Logical Operators in PHP
  2. PHP Call Function
  3. Prime Numbers in PHP
  4. Armstrong Number in C#

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • PHP Object Injection
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP list
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP strpos
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More