Sunday, August 5, 2012

PHP : Palindrome Number Program

Here is a simple PHP program which checks whether the given number is palindrome or not.

1) Here is the form to enter the number.
 
<html>
    <head>
        <title>INTEGER PALINDROME</title>
    </head>
    <body>
        <table width="100%" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    <form name="palindrome" method="GET" action="palindrome.php">
                        <fieldset>
                            <ol>
                                <li>
                                    <label>ENTER THE INTEGER NUMBER:</label>
                                    <input type="text" name="Num" value=" ">
                                </li>
                                <li>
                                    <input type="submit" name="check" value="submit"><br>
                                </li>
                            </ol>
                        </fieldset>    
                    </form>
                </td>
            </tr>
        </table>
    </body>
</html>   

2) Now here is the Palindrome.php which gets the number and checks whether it  is palindrome or not.

<?php
$n = $_GET['Num'];
function intpal($n1)
{
    $sum = 0;
    $nn = $n1;
    while ($nn > 0) {
        $sum = $sum * 10;
        $sum = $sum + $nn % 10;
        if (($nn % 10) <> 0)
            $nn = ceil($nn / 10) - 1;
        else
            $nn = $nn / 10;
    }
    if ($sum == $n1)
        echo"INTEGER $n1 IS Palindrome ";
    else
        echo"INTEGER $n1 IS NOT Palindrome ";
}
if (empty($n)) {
    echo"ENTER VALID INPUT";
    exit;
}
intpal($n);
?>    
             

No comments:

Post a Comment

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...