Jump to content

Armstrong Number In Php


prathameshkakade
Go to solution Solved by kicken,

Recommended Posts

Hello everyone.

I tried to find out wether a number is armstrong or not.My logic was right but few php errors are creeping.I am new to PHP.So please help me out.

Here is the code

armstrong.php

<html>
<head>
<title>
Armstrong number
</title>
</head>
<body>
<?php
$arm=0;
$i;
$k;
$i=$_POST["i"];
$j=$i;
while($j!=0)
{
$k=$j%10;
$arm=$arm+($k*$k*$k);
$j=$j/10;
}
if($i==$arm)
{echo "Yes it is an Armstrong number";}
else
{echo "No,it is not an Armstrong Number";}
?>
<form method="POST" action="armstrong.php">
<input type="text" name="i" />
<br>
<br>
<input type="submit" name="submit" />
<input type="reset" name="reset" />
</form>
</body>
</html>

 

The error is it shows "Yes it is an Armstrong number" by default when we open armstrong.php in browser.And when we enter a number and click submit,every thing goes fine.

Here is a screenshot of it.

8965345ac30891d41da6a57f6732a47a.PNG

<hr>

 

 

 

 

ad3dcaeda45662d4cedd6bd5711e3b40.PNG

And also some php errors are visible(undefined variable.....).

Please help.

Link to comment
Share on other sites

  • Solution

You need to make sure that your code only runs if you have submitted a number. You can do that by wrapping the whole block of code in a if() statement that checks whether $_POST['i'] exists or not

 

if (isset($_POST['i'])){
....
}

 

Link to comment
Share on other sites

You need to make sure that your code only runs if you have submitted a number. You can do that by wrapping the whole block of code in a if() statement that checks whether $_POST['i'] exists or not

 

if (isset($_POST['i'])){
....
}

 

Thanks man.It worked out perfectly.But can you tell me why we added that lines to code?I mean the function of (isset($_POST['i'])).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.