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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
The logic behind Armstrong number:
Below are the examples using various methods such as: for, while, do-while.
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:
Output:
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:
Output:
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:
Output:
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:
Output:
Code:
Output:
Here the form basic concept is to include the user input. User can enter whatever input value he wants to enter.
Code:
Output:
The above is the detailed content of Armstrong Number in PHP. For more information, please follow other related articles on the PHP Chinese website!