Featured Post Today
print this page
Latest Post
Subscribe To Our Newsletter

Sign Up Now To Get Free Coupon Codes, Event Coupon Codes Updates, Offers Updates. It's 100% Free!

We Hate Spam! Really, It's terrible and we never do it.

Showing posts with label PHP Assignments. Show all posts
Showing posts with label PHP Assignments. Show all posts

Palindrome an other script with using functions.

Palindrome an other script with using functions. Previous Palindrome Script was coded without using any function. In this function 3 methods are used,

  1. str_split() function
  2. var_dump() function
  3. implode() function
User will input any value consist on 5 digits, then str_split() will split your word / sentence to array. And 2nd var_dump() function will show the value, type of array, then implode() function is used to combine the value. 
check this code. try it
My friend Dan Edge helped me in it.
I thank to him.


 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">   
   <!-- to take multiple inputs copy the below line and change name="input" attribute -->   
   Enter input 1:&nbsp;<input type="text" name="input" value="" /><br />   
   <input type="submit" name="submit" value="Submit" />   
 </form>   
 <?php  
   $inputs = array();   
   if (isset($_POST['input'])) //to check if the form was submitted   
   {   
     $inputs = str_split($_POST['input']);  
     var_dump($inputs);   
     if (count($inputs) == 5)   
     {      
       if(($inputs[0]==$inputs[4])&&($inputs[1]==$inputs[3]))   
       {   
         echo implode($inputs) . ' is a palindrome number. <br />';   
       }   
       else  
       {   
         echo implode($inputs) . ' is not a palindrome number. <br />';   
       }                       
     }                
     else   
     {   
       echo "Try Again : Enter 5 Digit Number. :(";   
     }   
   }   
 ?>  
0 comments

Write an application that reads in a five-digit integer and determines whether it’s a palindrome. If the number is not five digits long, display an error message and allow the user to enter a new value.

I used many functions in this script. like Mod, Divide, Array Value Assigning, Float Function, then I used simple If .. else.. If conditional statement, and if within in statement.  
(Palindromes) A palindrome is a sequence of characters that reads the same backward as
forward.
For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and
11611.

Try this code. I hope you will learn from it.


 <?PHP  
      $inputs = array();  
      if (isset($_POST['submit'])) { //to check if the form was submitted  
           $value = isset($_POST['input0'])? $_POST['input0'] : null;  
           for($count = 1; $value != null; $count++){  
                array_push($inputs, $value);  
                $value = isset($_POST['input'.$count])? $_POST['input'.$count] : null;  
           }  
      }  
      var_dump($inputs);  
 ?>       
 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
   <!-- to take multiple inputs copy the below line and change name="input" attribute -->  
   Enter input 1:&nbsp;<input type="text" name="input0" value="" /><br />  
   <input type="submit" name="submit" value="Submit" />  
 </form>  
 <?php  
 $dig = $inputs[0];  
 /*Now I need to get Mod 5th times from remaining value*/  
   $dig5 = ($dig % 10); //as mod of 10201 is 1 : $dig5 is Digit 5  
     $inputs[5] = $dig5; //1 is saved in 5th index  
   $dig4 = ($dig / 10); //value like 1020.1 : $dig4 is Digit 4  
     $dig4v = floor($dig4); //It will make 1020 : $dig4v is Digit 4th Value  
       $dig4vi = ($dig4v % 10); // as Mode of 1020 is 0 : $dig4vi is Digit 4th value index  
         $inputs[4] = $dig4vi; //0 is saved in 4th index  
   $dig3 = ($dig4v / 10); //value like 102.0 : $dig3 is Digit 3  
     $dig3v = floor($dig3); //It will make 102 : $dig3v is Digit 3rd Value  
       $dig3vi =($dig3v % 10); // as Mode of 102 is 2 : $dig3vi is Digit 3rd value index  
          $inputs[3] = $dig3vi; //2 is saved in 3rd index  
   $dig2 = ($dig3v / 10); //value like 10.2 : $dig2 is Digit 2  
     $dig2v = floor($dig2); //It will make 10 : $dig2v is Digit 2nd Value  
       $dig2vi = ($dig2v % 10); // as Mode of 10 is 0 : $dig2vi is Digit 2nd value index  
         $inputs[2] = $dig2vi; //0 is saved in 2rd index  
   $dig1 = ($dig2v / 10); //value like 1.0 : $dig1 is Digit 1  
     $dig1v = floor($dig1); //It will make 1 : $dig1v is Digit 1st Value  
       $inputs[1] = $dig1v; //1 is saved in 1st index  
 if (($inputs[0]>=10000)&&($inputs[0]<=99999))  
   {       
       if(($inputs[1]==$inputs[5])&&($inputs[2]==$inputs[4]))  
         {  
           echo "{$inputs[0]} is a palindrome number. <br />";  
         }  
       elseif(($inputs[1]!=$inputs[5])||($inputs[2]!=$inputs[4]))  
         {  
           echo "{$inputs[0]} is not a palindrome number. <br />";  
         }                                          
   }                           
 else  
   {  
     echo "Try Again : Enter 5 Digit Number. :(";  
   }  
 ?>  
0 comments

Write a script for Square of Asterisks?

I used nested loop in this script for creating square of Asterisks. I used for loop 1st then i used while loop within for loop and then I used if conditional statement to create break.
Try this code.


 <?PHP  
      $inputs = array();  
      if (isset($_POST['submit'])) { //to check if the form was submitted  
           $value = isset($_POST['input0'])? $_POST['input0'] : null;  
           for($count = 1; $value != null; $count++){  
                array_push($inputs, $value);  
                $value = isset($_POST['input'.$count])? $_POST['input'.$count] : null;  
           }  
      }  
      var_dump($inputs);  
 ?>       
 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
   <!-- to take multiple inputs copy the below line and change name="input" attribute -->  
   Enter input 1:&nbsp;<input type="text" name="input0" value="" /><br />  
   <input type="submit" name="submit" value="Submit" />  
 </form>  
 <?php  
   for ($a=1 ; $a<= $inputs[0]; $a++)  
   {  
   $x = $inputs[0];  
   $y = 1;  
   while ($y <= $x)  
   {  
     echo "*";  
     if($y==$x)  
     {  
       echo "<br />";  
     }  
     $y++;  
   }  
   }  
 ?>  
0 comments

Write a script that prompts the user to enter the size of the side of a square and then displays a hollow square of that size made of asterisks. Your script should work for squares of all side lengths between 1 and 20.

For making Hollow Square of Asterisks, I need to use 3 times for loop. It is easy ; check this script.
You should try it.
User will enter a value and will get exact numbers of Asterisks Hollow Square:


 <?PHP  
      $inputs = array();  
      if (isset($_POST['submit'])) { //to check if the form was submitted  
           $value = isset($_POST['input0'])? $_POST['input0'] : null;  
           for($count = 1; $value != null; $count++){  
                array_push($inputs, $value);  
                $value = isset($_POST['input'.$count])? $_POST['input'.$count] : null;  
           }  
      }  
      var_dump($inputs);  
 ?>       
 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
   <!-- to take multiple inputs copy the below line and change name="input" attribute -->  
   Enter input 1:&nbsp;<input type="text" name="input0" value="" /><br />  
   <input type="submit" name="submit" value="Submit" />  
 </form>  
 <?php  
 $less = ($inputs[0]-2);  
 for ($fl=1; $fl<=$inputs[0]; $fl++)  
   {  
     echo "*";  
     if ($fl==$inputs[0])  
       {  
         echo "<br />";  
       }  
   }  
 for ($hstr=1; $hstr<=$less; $hstr++)  
   {  
     echo "*" . str_repeat("&nbsp;", $less) . "* <br />";  
   }  
 for ($ll=1; $ll<=$inputs[0]; $ll++)  
   {  
     echo "*";  
   }  
 ?>  
0 comments

(Validating User Input) For any input, if the value entered is other than 1 or 2, keep taking input until the user enters a correct value.

Assignment is about to show that number is 1 and 2 or digit.
I used if else if statement here. and find great function.
Note : use this code. and try from different ways.


 <?PHP  
      $inputs = array();  
      if (isset($_POST['submit'])) { //to check if the form was submitted  
           $value = isset($_POST['input0'])? $_POST['input0'] : null;  
           for($count = 1; $value != null; $count++){  
                array_push($inputs, $value);  
                $value = isset($_POST['input'.$count])? $_POST['input'.$count] : null;  
           }  
      }  
      var_dump($inputs);  
 ?>       
 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
 <!-- to take multiple inputs copy the below line and change name="input" attribute -->  
 Enter input 1:&nbsp;<input type="text" name="input0" value="" /><br />  
 <input type="submit" name="submit" value="Submit" />  
 </form>  
 <?php  
           $max = $inputs[0];  
           if ($max == 2)  
                {  
                     echo "Correct Value";  
                     die();  
                }  
           elseif ($max == 1)  
                {  
                     echo " Correct Value";  
                     die();  
                }  
           elseif ($max =! 1)  
                {  
                     echo " Not Correct Value. <br /> Try Again";  
                }  
           elseif ($max =! 2)  
                {  
                     echo " Not Correct Value. <br /> Try Again";  
                }  
           else  
                {  
                     echo "Try Again : Enter Digits";  
                }  
 ?>  
0 comments

Find Greatest Number from 10 inputs : PHP Assignment

I used input bar code in this assignment, which my teacher gave me to use. I used 10 input bars with different names. Then I used while loop and if conditional statement within loop to make this assignment successful.
Note : Try this code : Download Find Greatest Number Script


 <?PHP  
      $inputs = array();  
      if (isset($_POST['submit'])) { //to check if the form was submitted  
           $value = isset($_POST['input0'])? $_POST['input0'] : null;  
           for($count = 1; $value != null; $count++){  
                array_push($inputs, $value);  
                $value = isset($_POST['input'.$count])? $_POST['input'.$count] : null;  
           }  
      }  
      var_dump($inputs);  
 ?>       
 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
   <!-- to take multiple inputs copy the below line and change name="input" attribute -->  
   Enter input 1:&nbsp;<input type="text" name="input0" value="" /><br />  
   Enter input 2:&nbsp;<input type="text" name="input1" value="" /><br />  
   Enter input 3:&nbsp;<input type="text" name="input2" value="" /><br />  
   Enter input 4:&nbsp;<input type="text" name="input3" value="" /><br />  
   Enter input 5:&nbsp;<input type="text" name="input4" value="" /><br />  
   Enter input 6:&nbsp;<input type="text" name="input5" value="" /><br />  
   Enter input 7:&nbsp;<input type="text" name="input6" value="" /><br />  
   Enter input 8:&nbsp;<input type="text" name="input7" value="" /><br />  
   Enter input 9:&nbsp;<input type="text" name="input8" value="" /><br />  
   Enter input 10:&nbsp;<input type="text" name="input9" value="" /><br />  
   <input type="submit" name="submit" value="Submit" />  
 </form>  
 <?php  
     $max = $inputs[0]; //this variable to store values to array  
     $count = 1; //this variable to run the loop 10 times  
     while ($count < 10) //here it is Terminate condition  
     {  
         if ($max < $inputs[$count])  
         {  
           $max = $inputs[$count];  
         }  
       $count++;  
     }  
     echo "$max is greatest <br/>";  
 ?>  
1 comments

Write a script which prints all the odd numbers. Output: 1 3 5 7 9 …

I used while loop in this script with if conditional statement within while. Here I used Mod (%) in if and get results till 55. try this code.
Script to print all odd numbers. 


                          <?php  
                               $num=0;  
                while ($num<=55)  
                               {  
                                         if ($num % 2 == 1)  
                                         {  
                                          echo $num . "<br />";  
                                         }  
                                    $num++;  
                               }  
                          ?>  
0 comments

Write a script that calculates and prints the sum of the integers from 1 to 10. Use a while statement to loop through the calculation and increment statements. The loop should terminate when the value of x becomes 11.

I used ; while loop for it and used if condition within while loop and add break for 1st loop.
code is given bellow understand it.


                          <?php  
                $num = 0;  
                               while ($num+1)  
                                    {  
                                         echo $num . "<br />";  
                                              if ($num==10)  
                                              {  
                                                   break 1;  
                                              }  
                                         $num++;  
                                    }  
                          ?>  
0 comments
 
Support : | Internet Marketing Specialist And Business Developer
Copyright © 2013-2016. Samee Articles - All Rights Reserved
Proudly powered by Blogger