Thursday, 7 August 2014

Php small Program for beginners

Quest : Print
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

<?php
for ($i=1; $i<=5; $i++)
{
 for($j=1;$j<=$i;$j++)
 {
  echo $j." ";
 }
  echo "<br/>";   
}
?>




  Output:     1
  1 2
  1 2 3
  1 2 3 4
  1 2 3 4 5



Print

    *
   * *
  * * *
 * * * *
* * * * *


<?php
for ($i=1; $i<=5; $i++)
{
 for ($k=5; $k>$i; $k--)
 {
 //print one space throgh html ;
 echo " ";
 }
 for($j=1;$j<=$i;$j++)
 {
  echo "*";
 }
  echo "<br/>";
}
?>



Output:
    *



   * *

  * * *

 * * * *

* * * * *




What will be output of following program :

<?php
$x=89;
define('ABC',$x+1);
$x=$x+ABC;

echo ABC."<br/>";
echo $x;
 ?>

ans :
90
179

Find sum of even number between 1 to 100

<?php

$sum=0;
for($i=1;$i<=100;$i++)
{
if($i%2==0)
{
$sum=$sum+$i;
}
}

echo $sum;
?>
Output: 2550

Check given number is even or odd

<?php
$num=$_POST['n'];
if($num%2==0)
{
  echo $num." is even number";
}
else
{
  echo $num." is odd number";
}
?>

Check given character is vowel or consonent.

<?php
$char=$_POST['ch'];
if($char=="a")
{
  echo $char." is vowel";
}
elseif($char=="e")
{
  echo $char." is vowel";
}
elseif($char=="i")
{
  echo $char." is vowel";
}
elseif($char=="o")
{
  echo $char." is vowel";
}  
elseif($char=="u")
{
  echo $char." is vowel";
}
else
{
echo $char. "is consonent";
}
?>




Write a program to print your name 10 times.


<?php
$name="Pooja";
for ($i=1; $i<=10; $i++)
{
echo "My Name is: ".$name."<br/>"
}
?>



Output: My Name is Pooja
My Name is Pooja
My Name is Pooja
My Name is Pooja
My Name is Pooja




Find the sum of 1 to 100.


<?php
$sum=0;
for ($i=1; $i<=100; $i++)
{
   $sum=$sum+$i;
  
}
echo $sum;
?>



Output: 5050




Find all even numbers between 1 to 100 using loop(Not use conditional statement).


<?php
for ($i=2; $i<=100; $i+=2)
{
   echo $i." ";
  
}
?>



  Output: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42
44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86
88 90 92 94 96 98 100




Find all odd numbers between 1 to 100 using loop.


<?php

for ($i=1; $i<=99; $i+=2)
{
   echo $i." ";
  
}
?>



  Output: 1 3 5 7 9 ... 99




Find the Sum of even and odd numbers between 1 to 100.


<?php
for ($i=1; $i<=100; $i++)
{
 if($i%2==0)
 {
   @$even=$even+$i;
 }
 else
 {
  @$odd=$odd+$i;
 }
  
}
echo "Sum of even numbers=".$even."<br/>";
echo "Sum of odd numbers=".$odd;
?>



  Output:  Sum of even numbers=2550
               Sum of odd numbers=2500




Add two numbers using loop(Not use + operator).


<?php
@$f=$_GET['f'];
@$s=$_GET['s'];
for ($i=1; $i<=$s; $i++)
{
 $f++;
}
echo "Sum of given numbers=".$f;
?>

<body>
<form>
 Enter first number
 <input type="text" name="f"><br/>
 Enter Second number
 <input type="text" name="s"><br/>
 <input type="submit" value="add">
</form>
</body>
 


  Output:      Sum of given numbers=1000

  Enter first number

  Enter Second number





Subtract two numbers using loop(Not use - operator).


<?php
@$f=$_GET['f'];
@$s=$_GET['s'];
for ($i=1; $i<=$s; $i++)
{
 $f--;
}
echo "Subtraction of given numbers=".$f;
?>

<html>
<body>
<form>
 Enter first number
 <input type="text" name="f"><br/>
 Enter Second number
 <input type="text" name="s"><br/>
 <input type="submit" value="Subtract">
</form>
<body>
 


  Output:      Subtraction of given numbers=1000
        Enter first number
Enter Second number
            


Write a program to display table of given number.


<?php
@$tab=$_GET['tab'];
$i=1;
do
 {
  $t=$tab*$i;
echo $t." ";
 
  $i++;
 
 }
while ($i<=10);
 ?>

<body>
<form>
 Enter Your table
 <input type="text" name="tab"><br/>
  <input type="submit" value="Table">
</form> </body>



















No comments:

Post a Comment

Printing first 50 Fibonacci numbers Using PHP Program

Fibonacci series is a series of numbers in which each number is the sum of the two preceding numbers. For example. 0 , 1 , 1 , 2 , 3 , 5...