forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Armstrong Number in PHP. (jainaman224#2295)
- Loading branch information
1 parent
c167591
commit 7fc1c00
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
function isArmstrongNumber($number){ | ||
// Initial Sum is equal to 0; | ||
$sum = 0; | ||
$x = $number; | ||
while($x != 0) | ||
{ | ||
$rem = $x % 10; | ||
$sum = $sum + ($rem*$rem*$rem); | ||
$x = $x / 10; | ||
} | ||
|
||
// Armstrong number Condition ==> given number = SumOf(cubeOf(single Digit)) | ||
if ($number == $sum) | ||
return true; | ||
|
||
// not an armstrong number | ||
return false; | ||
} | ||
|
||
// Driver Code | ||
$number = 333; | ||
$flag = isArmstrongNumber($number); | ||
if ($flag == true) | ||
echo "Given number is an Armstrong Number"; | ||
else | ||
echo "Given number is not an Armstrong Number"; | ||
|
||
?> | ||
|
||
/** | ||
OUTPUT: | ||
Given Number is not an ArmStrong Number. | ||
*/ |