首頁  >  文章  >  後端開發  >  PHP 中的阿姆斯壯數

PHP 中的阿姆斯壯數

WBOY
WBOY原創
2024-08-29 13:13:55910瀏覽

阿姆斯壯數是一種數字,其值/數字等於每個數字的立方之和。這些類型的數字稱為阿姆斯特朗數字。一些阿姆斯壯數字是 0、1、153、371、407、471 等

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

阿姆斯壯數背後的邏輯:

  • 首先,你必須輸入數字來檢查它是否是阿姆斯壯。
  • 將該數字儲存在變數中。
  • 現在將該變數求和。
  • 現在將該數字除以 10 值,直到商數為 0。
  • 求餘數的立方。
  • 比較和的變數以及數字變數值(如果兩個數字相同則為阿姆斯壯數)。

在 PHP 中檢查阿姆斯壯數的範例

以下是使用各種方法的範例,例如:for、while、do-while。

範例 #1:在 PHP 中使用 For 迴圈

這個程式是使用For迴圈來檢查數字是否是阿姆斯壯數。在下面的 PHP 程式中,輸入數字儲存在 armnum2 變數中,並將 0 指派給total3 變數。現在,在For 循環中使用初始化、增量和條件對新變數「x3」進行賦值,將Armnum2 變數分配給x3 作為起始編號進行初始化,條件為x3!=0 退出循環,透過將x3 除以10並儲存在x3 值中進行增量。

Rem3變數是取得餘數。現在,在 For 迴圈中對餘數進行立方,使用 For 迴圈的初始化、增量和條件值來取得所有餘數,因為作為邏輯,輸入數字和數字數字的立方應該等於確認為阿姆斯壯數。

代碼:

<?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";
}
?>

輸出:

PHP 中的阿姆斯壯數

範例#2:使用 HTML 表單和 For 迴圈程式

這裡表單的基本概念是在 For 迴圈的幫助下包含使用者輸入。執行PHP表單for循環腳本後,使用者可以藉助瀏覽器中可見的提示輸入任何他想要輸入的輸入值。使用下面的程式碼檢查一下就知道了。

這是一個帶有 HTML 表單的 For 循環程序,使用 Post 方法獲取使用者的直接使用者輸入。 Form方法有post,輸入參數為數字,使用submit,Number是將輸入的數字傳遞給程序,檢查數字/變數值是否為阿姆斯壯數。之後,像上面一樣的循環程序繼續檢查阿姆斯特朗數。所有程序也是如此。

代碼:

<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";
}
}
?>

輸出:

PHP 中的阿姆斯壯數

範例 #3:在 PHP 中使用 While 迴圈

這是一個While循環程序,用於檢查數字是否是阿姆斯壯數。由於 x1 不等於 0,因此退出迴圈的條件包含在 While 迴圈內部。 Rem1 變數被指派以取得餘數值。透過使用餘數值及其立方,直到條件 x1 等於 0。然後 x1 是將輸入數字除以 10 並儲存在 x1 變數中,以使用 While 迴圈來取得所有餘數值。同樣的事情也適用於 Do While 迴圈程式。

代碼:

<?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";
}
?>

輸出:

PHP 中的阿姆斯壯數

範例#4:使用 HTML 表單和 While 循環程式

這裡表單的基本概念是包含使用者輸入。使用者可以輸入他想要輸入的任何輸入值。看看下面的程式碼就知道了。

代碼:

<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";
}
}
?>

輸出:

PHP 中的阿姆斯壯數

範例 #5:在 PHP 中使用 DO-While 迴圈

代碼:

<?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";
}
?>

輸出:

PHP 中的阿姆斯壯數

範例 #6:使用 HTML 表單和 Do While 迴圈

這裡表單的基本概念是包含使用者輸入。使用者可以輸入任何他想要輸入的輸入值。

代碼:

<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";
}
}
?>

輸出:

PHP 中的阿姆斯壯數

以上是PHP 中的阿姆斯壯數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP 中的質數下一篇:PHP 中的質數