Monday, April 24, 2017

PHP script for printing first 20 Fibonacci numbers .

<?php
$count = 0 ;
$f1 = 0;
$f2 = 1;
echo $f1." , ";
echo $f2." , ";
while ($count < 20 )
{
$f3 = $f2 + $f1 ;
echo $f3." , ";
$f1 = $f2 ;
$f2 = $f3 ;
$count = $count + 1;
}
?>

Display Prime Numbers Between two Intervals in C language.

#include <stdio.h>
int main()
{
    int low, high, i, flag;
    printf("Enter two numbers(intervals): ");
    scanf("%d %d", &low, &high);

    printf("Prime numbers between %d and %d are: ", low, high);

    while (low < high)
    {
        flag = 0;

        for(i = 2; i <= low/2; ++i)
        {
            if(low % i == 0)
            {
                flag = 1;
                break;
            }
        }

        if (flag == 0)
            printf("%d ", low);

        ++low;
    }

    return 0;
}

Check prime no using function.

    <?php 
    function IsPrime($n) 
    { 
     for($x=2; $x<$n; $x++) 
       { 
          if($n %$x ==0) 
              { 
               return 0; 
              } 
        } 
      return 1; 
       } 
    $a = IsPrime(13); 
    if ($a==0) 
    echo 'This is not a Prime Number.....'."\n"; 
    else 
    echo 'This is a Prime Number..'."\n"; 
    ?> 

Sunday, April 23, 2017

Program to find prime numbers in between a range

<?php
//error_reporting(E_ALL);
//Program to find prime numbers in between a range

$num =23;

for( $j = 2; $j <= $num; $j++ )
{
for( $k = 2; $k < $j; $k++ )
{
if( $j % $k == 0 )
{
break;
}

}
if( $k == $j )
echo "Prime Number : ", $j, "<br>";
}
?>

PHP Program to find Largest & Smallest numbers in an Array

<html>
<head>
<title>Find Largest & Smallest numbers in an Array - PHP Programming</title>
</head>
<body>
   
    <h3>PHP Program to find Largest & Smallest numbers in a Array</h3>
    Enter the Numbers separated by Commas <br />
    (eg: 12,23,56)
    <br /><br />
    <form method="post">
        <input type="text" name="numbers"/>
        <button type="submit">Check</button>
    </form>
</body>
</html>

<?php
   
    if($_POST)
    {
        //get the post value from form
        $numbers = $_POST['numbers'];
       
        //separate the numbers and make into array
        $numArray = explode(',', $numbers);
       
        //assign the first value of the above array for the Largest & Smallest variables
        $largest  = $numArray[0];
        $smallest = $numArray[0];
       
       
        //now loop through each numbers and find which is Largest & Smallest numbers in a PHP Array
        foreach($numArray as $num){
           
           
            if($num > $largest){
                $largest = $num;
            }
            else if($num < $smallest){
                $smallest = $num;
            }
           
        }
               
        //print the output

        echo "Largest Number is: $largest <br />";
        echo "Smallest Number is: $smallest";

    }    

?>

Friday, April 21, 2017

Sort an array of integers without using PHP built-in sort functions

<?php

$srtArray=array(2,8,9,5,6,3);
 for ($i=0; $i<count($srtArray); $i++) {
    for ($j=0; $j<count($srtArray); $j++) {
      // Compare two elements of array
      if ($srtArray[$j] > $srtArray[$i]){
        $tmp = $srtArray[$i];
        $srtArray[$i] = $srtArray[$j];
        $srtArray[$j] = $tmp;
      }
    }
 }
//Print an array after sorting
 for($i=0;$i<count($srtArray);$i++){
   echo $srtArray[$i]."<br>\n";
 }
   
?>

Monday, July 18, 2016

.htaccess Redirection Rule

This .htaccess file will redirect http://example.com/folder1/ to http://example.com/folder2/. Choose this version if you don't have the same file structure in both directories:
Filename: .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1.*$ http://example.com/folder2/ [R=301,L]
  • This .htaccess file will redirect http://example.com/folder1/ to plain http://example.com/. Choose this version if you want people redirected to your home page, not whatever individual page in the old folder they originally requested:
Filename: .htaccess.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1.*$ http://example.com/ [R=301,L]
  • This .htaccess file will redirect http://example.com/folder1/file.html tohttp://example.com/folder2/file.html. Choose this version if your content is duplicated in both directories:
File name: .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1/(.*)$ http://gs.mt-example.com/folder2/$1 [R=301,L]

Test

Upload this file to folder2 (if you followed the first or third example) or your html folder (if you followed the second example) with FTP:
Filename: index.html
<html>
<body>
Mod_rewrite is working!
</body>
</html>
Then, if you followed the first or second example, visit http://example.com/folder1/ in your browser. You should see the URL change to http://example.com/folder2/ orhttp://example.com/ and the test page content.
If you followed the third example, visit http://example.com/folder1/index.html. You should be redirected to http://example.com/folder2/index.html and see the test page content.