Assignment Five Writing Programs Using PHP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

ASSIGNMENT FIVE: WRITING PROGRAMS USING PhP

Subtraction in Simple Code


Subtraction of two numbers 30 and 15 is shown.
Example:
1. <?php
2. $x=30;
3. $y=15;
4. $z=$x-$y;
5. echo "Difference: ",$z;
6. ?>
Output:

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:

Even Odd Program using Form in PHP


By inserting value in a form we can check that inserted value is even or odd.
5
Example:
1. <html>
2. <body>
3. <form method="post">
4. Enter a number:
5. <input type="number" name="number">
6. <input type="submit" value="Submit">
7. </form>
8. </body>
9. </html>
10.<?php
11. if($_POST){
12. $number = $_POST['number'];
13. //divide entered number by 2
14. //if the reminder is 0 then the number is even otherwise the number is o
dd
15. if(($number % 2) == 0){
16. echo "$number is an Even number";
17. }else{
18. echo "$number is Odd number";
19. }
20. }
21.?>
Output:
On entering the number 23456, following output appears.

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.

Prime number in PHP


Example:
Here is the Program to list the first 15 prime numbers.
1. <?php
2. $count = 0;
7
3. $num = 2;
4. while ($count < 15 )
5. {
6. $div_count=0;
7. for ( $i=1; $i<=$num; $i++)
8. {
9. if (($num%$i)==0)
10.{
11.$div_count++;
12.}
13.}
14.if ($div_count<3)
15.{
16.echo $num." , ";
17.$count=$count+1;
18.}
19.$num=$num+1;
20.}
21.?>
Output:

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.

There are two ways to find factorial in PHP:


o Using loop
o Using recursive method
Logic:
o Take a number.
o Take the descending positive integers.
o Multiply them.

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

Area of Triangle in PHP

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:

Area of Triangle with Form in PHP


Program to calculate area of triangle by inserting values in the form is shown.
Example:
1. <html>
2. <body>
3. <form method = "post">
4. Base: <input type="number" name="base">
5. <br><br>
6. Height: <input type="number" name="height"><br>
7. <input type = "submit" name = "submit" value="Calculate">
8. </form>
9. </body>

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 String using strrev() function


A reverse string program using strrev() function is shown.
Example:
1. <?php
2. $string = "JAVATPOINT";
3. echo "Reverse string of $string is " .strrev ( $string );
4. ?>
Output:
21
Reverse String Without using strrev() function
A reverse string program without using strrev() function is shown.
Example:
1. <?php
2. $string = "JAVATPOINT";
3. $length = strlen($string);
4. for ($i=($length-1) ; $i >= 0 ; $i--)
5. {
6. echo $string[$i];
7. }
8. ?>
Output:

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.

Reversing Number in PHP


Example:
Below progrem shows digits reversal of 23456.
1. <?php
2. $num = 23456;
3. $revnum = 0;
4. while ($num > 1)
5. {
6. $rem = $num % 10;
7. $revnum = ($revnum * 10) + $rem;
8. $num = ($num / 10);
9. }
10.echo "Reverse number of 23456 is: $revnum";
11.?>
Output:

Reversing Number With strrev () in PHP

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

You might also like