Showing posts with label Useful PHP Programs. Show all posts
Showing posts with label Useful PHP Programs. Show all posts

Monday, August 6, 2018

phpMyPassion

Armstrong Number Program in PHP

PhpMyPassion
"Armstrong Number Program" is the initial program from where most of the beginner developers will start to learn and write code in any language. An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits is equal to the number itself.
Here is an example of check a armstrong number in PHP.

Armstrong Number Program :-

Below is the simple PHP program to check a number armstrong.

<?php

$number=153;
$sum=0;
$temp=$number;
while($temp!=0)
{
$reminder=$temp%10;
$sum=$sum+$reminder*$reminder*$reminder;
$temp=$temp/10;
}
if($number==$sum)
{
echo "It is an Armstrong number";
}
else
{
echo "It is not an armstrong number";
}

?>

Output :-

It is an Armstrong number
Read More

Saturday, December 30, 2017

phpMyPassion

Prime Number Program in PHP


Prime Number :- 

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 3 is a prime number because it has no positive divisors other than 1 and 3.

Prime numbers are below:-

2, 3, 5, 7, 11, 13, 17, 19 ....

Here I am writing the PHP program to check whether a number is Prime or not.

<?php
function CheckPrime($number){
    for($x=2;$x<$number;$x++){
        if($number%$x == 0){
            return 0;
        }
    }
    return 1;
}

$a = CheckPrime(17);
if ($a==0)
    echo 'This is not a Prime Number.....'."\n";
else
    echo 'This is a Prime Number..'."\n";
?>

Output :- This is a Prime Number..

Using the above program you can check whether a number is prime or not in PHP.

Read More
phpMyPassion

Palindrome Number Program in PHP


Palindrome Number :-

A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed. Like 53235, 

Below are the example palindrome number:-

121 , 212 , 12321 , 16461

#Steps to Check a Palindrome Number :-

  • Take a Number
  • Now reverse its digit
  • Now compare both the numbers
  • if both are equals to each other then return "Number is Palindrome".
  • else return "Number is not Palindrome"

Here I am writing the PHP program to check whether a number is palindrome or not.

Palindrome Number Program Without of Using PHP Predefined Function :-

<?php
$number = 53235;
$p = $number;
$revnum =0;
while($number != 0){ //echo 'i am';
      $revnum = $revnum*10 + $number % 10 ;//echo '-';
     $number = (int)($number/10);
    //echo '<br/>';
}
if($revnum==$p){
    echo $p.' is palindrome number';
}
else{
    echo 'number is not palindrome';
}
?>


Output :- 53235 is palindrome number

Using function that take a number as an input :-

<?php 
function checkPalindrome($number){
    $p = $number;
    $revnum =0;
    while($number != 0){ 
          $revnum = $revnum*10 + $number % 10 ;
         $number = (int)($number/10);
    }
    if($revnum==$p){
        echo $p.' is palindrome number';
        //return TRUE;
    }
    else{
        echo 'number is not palindrome';
        //return FALSE;
    }
}
echo checkPalindrome('121');
?>

Output :- 121 is palindrome number

Using PHP Predefined Function :-

<?php 
$number = '121';

if(strrev($number) == $number){
    echo $number.' is palindrome number';
}
else{
    echo 'number is not palindrome';
}
?>


Output :- 121 is palindrome number

Using the above program you can check whether a number is palindrome or not in PHP.

Read More

Friday, December 29, 2017

phpMyPassion

Fibonacci Series in PHP


Fibonacci series means to get next term by adding its previous two numbers.

For an example:-

0 1 1 2 3 5 8 13 21

Here I am writing the PHP program to print Fibonacci Series.

<?php

function printFabonacciSeries($number){
    $a = 0; // first number of series
    $b = 1; // second number of series
    echo 'Fabonacci Series <br/>';
    echo $a.' '.$b;
    for($i=2;$i<$number;$i++){
        $c = $a + $b; //print next number of series
        echo ' '.$third;
        $a = $b;
        $b = $c;
    }
}
printFabonacciSeries(9);
?>

Output :- 0 1 1 2 3 5 8 13 21

Alternate Way to Print Fabonacci Series :-

<?php
$number = 9;
$first = 0;
$second = 1;
echo "$first $second "; // 0 1

for($i=1;$i<= $number-2 ;$first=$second,$second=$third,$i++ ) 
{
  echo $third = $first+$second; //print next number of series
  echo " ";
}
?>

Using the above program you can find Fibonacci Series in PHP.

Read More

Tuesday, December 5, 2017

phpMyPassion

Factorial Program in PHP

The factorial of number n is multiplying given integer with the sequence of its descending positive integers. we denoted the factorial of a number n by n! in mathematics. Here I am writing factorial program in PHP..

image source : google images
Example :-

5! = 5*4*3*2*1 = 120  

7! = 7*6*5*4*3*2*1 = 5040  


Here are writing the PHP program to find a factorial of a number.


<?php
function factorial($number){
    $factorial = 1;
    for($x=$number;$x>=1;$x--){
        $factorial = $factorial*$x;
    }
    return $factorial;
}

echo 'factorial is='.factorial(11);
?>

Output :- factorial is=3,99,16,800

Using the above program you can find the factorial of any positive integer in PHP.

Read More