Monday 26 February 2018

PHP Programs For Armstrong number

Question: Enter a number and check whether the number is?
1)    Armstrong number or not
2)     Calculate Factorial
3)    Prime number or not
4)    Calculate Sum of Digits of a number
5)    Calculate Fibonacci value
6)    Palindrome  number or not
7)    Reverse number or not

Solution:
numops.html
<html>
<title>Number Operations</title>
<body>
<h1 style="text-align:center; color: Red"> Number operations </h1>
<form action="numops1.php" method="POST">
<fieldset>
Enter the Number : <input type="number" name="num"><br><br>
<input type="submit" name="armst" value="Armstrong"/>
<input type="submit" name="fact" value="Factorial">
<input type="Submit" name="prime" value="Prime"/>
<input type="submit" name="sum" value="Sum of Digits">
<input type="submit" name="fibonacci" value="Fibonacci"/>
<input type="submit" name="palindrome" value ="Palindrome"/>
<input type="submit" name="rev" value ="Reverse"/>
</fieldset></br>

</form>
</body>
</html>
numops1.php
<?php
$num1=$_POST['num'];
$sum=0;
$rem=0;
$facto=1;
$pm=0;

if(isset($_POST['armst']))//Armstrong
{
          $temp=$num1;
                   while($temp!=0)
                   {
                             $rem=$temp%10;
                             $sum=$sum+$rem*$rem*$rem;
                             $temp=$temp/10;
                            
                   }
                   if($num1==$sum)
                   {
                             echo"Entered number $num1 is an Armstrong number";
                   }
                   else
                   {
                             echo"Entered number $num1 is NOT an Armstrong number";
                   }
}

if(isset($_POST['fact']))//Factorial
{
          while($num1>0)
          {
                   $facto=$facto*$num1;
                   $num1--;
          }
          echo"Factorial of given number  is : $facto";      
}

if(isset($_POST['prime']))//Prime
{
          for($i=1;$i<=$num1;$i++)
          {
                   if($num1%$i==0)
                   {
                             $pm++;
                   }
          }
          if($pm==2)
          {
                   echo"Number is a Prime number";
          }
          else
          {
                   echo"Number is NOT a Prime Number";
          }
         
}

if(isset($_POST['sum']))//Sum of digits
{
          $temp=$num1;
                   while($temp!=0)
                   {
                             $rem=$temp%10;
                             $sum=$sum+$rem;
                             $temp=$temp/10;
                            
                   }
                   echo"Sum of Digits is : $sum";
}                

if(isset($_POST['fibonacci']))
{
          $fst=0;
          $sec=1;
          echo "$fst<br> $sec<br>";
          for($i=2;$i<$num1;$i++)
          {
                   $thd=$fst+$sec;
                   echo "$thd<br>";
                  
                   $fst=$sec;
                   $sec=$thd;
          }
}

if(isset($_POST['palindrome']))
{
          $temp=$num1;
          $rev=0;
         
          while($num1!=0)
          {
                   $rev=$rev*10 + $num1%10;
                   $num1=(int)($num1/10);
          }
          if($rev==$temp)
          {
                   echo "$temp is a Palindrome";
          }
          else
          {
                   echo"$temp is NOT a Palindrome";
          }
}

if(isset($_POST['rev']))
{
          $temp=$num1;
          $rev=0;
         
          while($num1!=0)
          {
                   $rev=$rev*10 + $num1%10;
                   $num1=(int)($num1/10);
          }
          echo"Reverse of $temp is $rev";
         
}
?>
Output:

Number operations

Enter the Number : 

      

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...