Armstrong Number in PHP

WBOY
Release: 2024-08-29 13:13:55
Original
827 people have browsed it

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

Copy after login

Output:

Armstrong Number in PHP

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:

  
Enter Armstrong Number/Other:
Copy after login

Output:

Armstrong Number in PHP

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:

Copy after login

Output:

Armstrong Number in PHP

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:

  
Enter Armstrong Number/Other:
Copy after login

Output:

Armstrong Number in PHP

Example #5: Using DO-While Loop in PHP

Code:

Copy after login

Output:

Armstrong Number in PHP

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:

  
Enter Armstrong Number/Other:
Copy after login

Output:

Armstrong Number in PHP

The above is the detailed content of Armstrong Number in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!