Assignment Five Writing Programs Using PHP
Assignment Five Writing Programs Using PHP
Assignment Five Writing Programs Using PHP
Subtraction in Form
By inserting values in the form two numbers can be subtracted.
Example:
1. <html>
2. <body>
3. <form method="post">
4. Enter First Number:
5. <input type="number" name="number1" /><br><br>
1
6. Enter Second Number:
7. <input type="number" name="number2" /><br><br>
8. <input type="submit" name="submit" value="Subtract">
9. </form>
10.<?php
11. if(isset($_POST['submit']))
12. {
13. $number1 = $_POST['number1'];
14. $number2 = $_POST['number2'];
15. $sum = $number1-$number2;
16. echo "The difference of $number1 and $number2 is: ".$sum;
17. }
18.?>
19.</body>
20.</html>
Output:
2
Subtraction in Form without (-) Operator
By inserting values in the form two numbers can be subtracted but without using (-
) operator.
Example:
1. <body>
2. <form>
3. Enter First Number:
4. <input type="number" name="number1" /><br><br>
5. Enter Second Number:
6. <input type="number" name="number2" /><br><br>
7. <input type="submit" name="submit" value="Subtract">
8. </form>
9. </body>
10.<?php
11.
12.@$number1=$_GET['number1'];
13.@$number2=$_GET['number2'];
14.for ($i=1; $i<=$number2; $i++)
15.{
16. $number1--;
17.}
18.echo "Difference=".$number1;
19.?>
Output:
3
Even Odd Program
Even numbers are those which are divisible by 2. Numbers like 2,4,6,8,10, etc are
even.
Odd numbers are those which are not divisible by 2. Numbers Like 1, 3, 5, 7, 9, 11,
etc are odd.
Logic:
o Take a number.
o Divide it by 2.
o If the remainder is 0, print number is even.
4
Even Odd Program in PHP
A program to check 1233456 is odd or even is shown.
Example:
1. <?php
2. $number=1233456;
3. if($number%2==0)
4. {
5. echo "$number is Even Number";
6. }
7. else
8. {
9. echo "$number is Odd Number";
10.}
11.?>
Output:
6
On entering the number 285965, following output appears.
Prime Number
A number which is only divisible by 1 and itself is called prime number. Numbers
2, 3, 5, 7, 11, 13, 17, etc. are prime numbers.
o 2 is the only even prime number.
o It is a natural number greater than 1 and so 0 and 1 are not prime numbers.
8
Prime Number using Form in PHP
Example:
We'll show a form, which will check whether a number is prime or not.
1. <form method="post">
2. Enter a Number: <input type="text" name="input"><br><br>
3. <input type="submit" name="submit" value="Submit">
4. </form>
5. <?php
6. if($_POST)
7. {
8. $input=$_POST['input'];
9. for ($i = 2; $i <= $input-1; $i++) {
10. if ($input % $i == 0) {
11. $value= True;
12. }
13.}
14.if (isset($value) && $value) {
15. echo 'The Number '. $input . ' is not prime';
16.} else {
17. echo 'The Number '. $input . ' is prime';
18. }
19.}
20.?>
Output:
On entering number 12, we get the following output. It states that 12 is not a prime
number.
9
On entering number 97, we get the following output. It states that 97 is a prime
number.
Factorial Program
The factorial of a number n is defined by the product of all the digits from 1 to n
(including 1 and n).
For example,
1. 4! = 4*3*2*1 = 24
2. 6! = 6*5*4*3*2*1 = 720
Note:
o It is denoted by n! and is calculated only for positive integers.
10
o Factorial of 0 is always 1.
The simplest way to find the factorial of a number is by using a loop.
Factorial in PHP
Factorial of 4 using for loop is shown below.
Example:
1. <?php
2. $num = 4;
3. $factorial = 1;
4. for ($x=$num; $x>=1; $x--)
5. {
6. $factorial = $factorial * $x;
7. }
8. echo "Factorial of $num is $factorial";
9. ?>
Output:
11
Factorial using Form in PHP
Below program shows a form through which you can calculate factorial of any
number.
Example:
1. <html>
2. <head>
3. <title>Factorial Program using loop in PHP</title>
4. </head>
5. <body>
6. <form method="post">
7. Enter the Number:<br>
8. <input type="number" name="number" id="number">
9. <input type="submit" name="submit" value="Submit" />
10.</form>
11.<?php
12. if($_POST){
13. $fact = 1;
14. //getting value from input text box 'number'
12
15. $number = $_POST['number'];
16. echo "Factorial of $number:<br><br>";
17. //start loop
18. for ($i = 1; $i <= $number; $i++){
19. $fact = $fact * $i;
20. }
21. echo $fact . "<br>";
22. }
23.?>
24.</body>
25.</html>
Output:
Area of Triangle
Area of a triangle is calculated by the following Mathematical formula,
1. (base * height) / 2 = Area
13
Program to calculate area of triangle with base as 10 and height as 15 is shown.
Example:
1. <?php
2. $base = 10;
3. $height = 15;
4. echo "area with base $base and height $height= " . ($base * $height) / 2;
5. ?>
Output:
14
10.</html>
11.<?php
12.if(isset($_POST['submit']))
13. {
14.$base = $_POST['base'];
15.$height = $_POST['height'];
16.$area = ($base*$height) / 2;
17.echo "The area of a triangle with base as $base and height as $height is $are
a";
18.}
19.?>
Output:
Star Triangle
The star triangle in PHP is made using for and foreach loop. There are a lot of star
patterns. We'll show some of them here.
Pattern 1
1. <?php
15
2. for($i=0;$i<=5;$i++){
3. for($j=5-$i;$j>=1;$j--){
4. echo "* ";
5. }
6. echo "<br>";
7. }
8. ?>
Output:
Pattern 2
1. <?php
2. for($i=0;$i<=5;$i++){
3. for($j=1;$j<=$i;$j++){
4. echo "* ";
5. }
6. echo "<br>";
7. }
8. ?>
Output:
16
Pattern 3
1. <?php
2. for($i=0;$i<=5;$i++){
3. for($k=5;$k>=$i;$k--){
4. echo " ";
5. }
6. for($j=1;$j<=$i;$j++){
7. echo "* ";
8. }
9. echo "<br>";
10.}
11.for($i=4;$i>=1;$i--){
12.for($k=5;$k>=$i;$k--){
13.echo " ";
14.}
15.for($j=1;$j<=$i;$j++){
16.echo "* ";
17.}
18.echo "<br>";
19.}
20.?>
17
Output:
Pattern 4
1. <?php
2. for($i=1; $i<=5; $i++){
3. for($j=1; $j<=$i; $j++){
4. echo ' * ';
5. }
6. echo '<br>';
7. }
8. for($i=5; $i>=1; $i--){
9. for($j=1; $j<=$i; $j++){
10.echo ' * ';
11.}
12.echo '<br>';
13.}
14.?>
Output:
18
Pattern 5
1. <?php
2. for ($i=1; $i<=5; $i++)
3. {
4. for ($j=1; $j<=5; $j++)
5. {
6. echo '* ';
7. }
8. echo "</br>";
9. }
10.?>
Output:
19
Pattern 6
1. <?php
2. for($i=5; $i>=1; $i--)
3. {
4. if($i%2 != 0)
5. {
6. for($j=5; $j>=$i; $j--)
7. {
8. echo "* ";
9. }
10.echo "<br>";
11.}
12.}
13.for($i=2; $i<=5; $i++)
14.{
15. if($i%2 != 0)
16.{
17. for($j=5; $j>=$i; $j--)
18.{
19.echo "* ";
20.}
21.echo "<br>";
22.}
23.}
24.?>
20
Output:
Reverse String
A string can be reversed either using strrev() function or simple PHP code.
For example, on reversing JAVATPOINT it will become TNIOPTAVAJ.
Logic:
o Assign the string to a variable.
o Calculate length of the string.
o Declare variable to hold reverse string.
o Run for loop.
o Concatenate string inside for loop.
o Display reversed string.
Reverse number
A number can be written in reverse order.
For example
12345 = 54321
Logic:
22
o Declare a variable to store reverse number and initialize it with 0.
o Multiply the reverse number by 10, add the remainder which comes after
dividing the number by 10.
23
Example:
Function strrev() can also be used to reverse the digits of 23456.
1. <?php
2. function reverse($number)
3. {
4. /* writes number into string. */
5. $num = (string) $number;
6. /* Reverse the string. */
7. $revstr = strrev($num);
8. /* writes string into int. */
9. $reverse = (int) $revstr;
10. return $reverse;
11.}
12. echo reverse(23456);
13.?>
Output:/strong>
24