Monday, 15 August 2016

PHP Basics : Armstrong Number

Hi dudes..

Lets check whether the number is armstrong using php.

Lets see what is armstrong number

lets take an example

Consider a number 153

take the cube of each number and add , if we get the same number then the given number is armstrong number

1 -- > 1*1*1 = 1
5 --> 5*5*5 = 125
3 -- >3*3*3 = 27

add 1+125+27 = 153

The given number 153 is an Armstrong Number


Lets begin your code





<?php
$given_number=371;
$sum=0;
//store the number in a temporary variable
$temp=$given_number;
while($temp!=0)
{
$remainder=$temp%10;
$sum=$sum+$remainder*$remainder*$remainder;
$temp=$temp/10;
}
if($given_number==$sum)
{
echo "Wow the given number is an Armstrong number";
}
else
{
echo "Oops!, It is not an Armstrong number";
}
?>



Its cool right ?

Soon we will go deep inside with PHP and we will start Python, Wordpress and So on..


No comments:

Post a Comment