Ad Code

Responsive Advertisement

Php Assignment 2 : Control Structures and Loops

  Assignment 2 : Control Structures and Loops

download this code file Assignment 2

Set A : 

1. Write a PHP Script to check whether a year is a leap or not.
answer =>
<!DOCTYPE html>
<html lang="en">
<head>
        <title>Leap Year</title>
</head>
<body>
    <form action="01_leap.php" method="post">
        <input type="number" name="year" id="" placeholder="Enter the leap year">
        <input type="submit" value="Send" name="submit">
    </form>
    <?php
    if(isset($_POST['submit'])) {
        $year = $_POST['year'];
        if($year % 4 == 0) {
            echo " <BR> $year is Leap year";
        } else {
            echo "<BR> $year is not Leap year";
        }
    }
    ?>
</body>
</html>
===========================================================
2. Write a PHP Script which will perform the Addition, Subtraction, Multiplication, and Division of two numbers as per the choice.
 (Use Switch Case) 
answer =>
HTML File : 02_arithmetic.html
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>A.M Operation</title>
        <link
            href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css"
            rel="stylesheet"
        />
    </head>
    <body>
        <form
            action="02_01_submit.php"
            method="post"
            style="
                width: 500px;
                margin: 1rem auto;
                border: 1px solid;
                padding: 1rem;
            "
        >
            <div class="mb-3">
                <label for="num1" class="form-label">Enter num 1 Value :</label>
                <input
                    type="number"
                    class="form-control"
                    id="num1"
                    name="num1"
                />
            </div>
            <div class="mb-3">
                <label for="num2" class="form-label">Enter num 2 Value :</label>
                <input
                    type="number"
                    class="form-control"
                    name="num2"
                    id="num2"
                />
            </div>
            <div class="mb-3">
                <p>Which operation to be perform</p>
                <select class="form-select" name="operation">
                    <option selected>Select the operation</option>
                    <option value="1" name="selected">Addition</option>
                    <option value="2" name="selected">Subtraction</option>
                    <option value="3" name="selected">Multiplication</option>
                    <option value="4" name="selected">Division</option>
                </select>
            </div>
            <button type="submit" class="btn btn-primary" name="submit">
                Submit
            </button>
        </form>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"></script>
    </body>
</html>
--------------------------------------------
PHP File :  02_01_submit.php
<?php
    if(isset($_POST['submit'])) {
       $val1 = $_POST['num1'];
       $val2 = $_POST['num2'];
       $operation_value = $_POST['operation'];
        switch ($operation_value) {
            case 1:
                $total = $val1 + $val2;
                echo "Addition of $val1 + $val2 = $total";
                break;
            case 2:
                $total = $val1 - $val2;
                echo "Subtraction of $val1 - $val2 = $total";
                break;
            case 3:
                $total = $val1 * $val2;
                echo "Multiplication of $val1 x $val2 = $total";
                break;
            case 4:
                $total = $val1 / $val2;
                echo "Division of $val1 / $val2 = $total";
                break;
                        default:
                echo "Invalid !";
        }
    }
?>
=========================================================
3. Write a PHP Script to display the grade of the student according to percentage. Use the 
following conditions: 
Percentage <40 => Grade=”Fail”
Percentage >= 40 and Percentage <=50 => Grade= “Pass Class”
Percentage >=50 and Percentage <=60 => Grade= “Higher Second Class”
Percentage >60 and Percentage <=70 => Grade= “First Class”
Percentage >70 => Grade= “First Class with Distinction”
answer => 
HTML File : grade.html
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Grade Calculate</title>
        <style>
            body {
                display: flex;
                align-items: center;
                justify-content: center;
                height: 100vh;
            }
            form {
                width: 400px;
                height: 300px;
                box-shadow: 3px 3px 8px rgba(0, 0, 0, 0.2);
                display: flex;
                align-items: center;
                justify-content: center;
                flex-direction: column;
            }

            input {
                padding: 10px 20px;
                margin: 10px 5px;
            }
        </style>
    </head>
    <body>
        <form action="03_stud_grade.php" method="post">
            <h2>Enter percentage</h2>
            <input
                type="number"
                name="percentage"
                id=""
                placeholder="Enter your percentage"
            />
            <input type="submit" value="Check" name="submit" />
        </form>
    </body>
</html>
-------------------------------
PHP File : grade.php 
<?php
    $per = $_POST['percentage'];
    if($per <= 40) {
        $grade = "Fail";
        echo "<BR>!! You Are Fail ";
    } else if($per >= 40 && $per <= 50) {
        echo "You are Pass <BR>";
        $grade = "Pass Class";
    } else if($per >= 50 && $per <= 60) {
        echo "You are Pass <BR>";
        $grade = "Higher Second Class";
    } else if($per > 60 && $per <= 70) {
        echo "You are Pass <BR>";
        $grade = "First Class";
    } else if($per > 70 && $per <= 100) {
        echo "You are Pass <BR>";
        $grade = "First Class with Distinction";
    } else {
        echo "Something Went Wrong <BR>";
        $grade = "NA";
    }
    echo "<H2>";
    echo "Your grade is $grade <BR>";
    echo "</H2>";
    ?>
===========================================================
Set B
1. Write a PHP Script to display prime numbers between 1 to 50.
answer =>
<?php
echo "Prime number between 1 to 50<BR>";
// $arr = array();
$i = 0;
$j = 0;
for ($i=1; $i <= 50; $i++) { 
    $n = prime($i); // function call
    if($n > 0) {
        echo $n."<BR>";
    }
 }
function prime($num) {
    for ($j=2; $j < $num; $j++) { 
        if($num % $j == 0) {
            return -1;
        } else {
            return $num;
        }
    }
}
?>
===========================================================
2. Write a PHP Script to display a perfect numbers between 1 to100.
answer => 
<?php
$i = 0;
$j = 0;
echo "Perfect Number between 1 to 100 <BR>";
for ($i=1; $i < 100; $i++) { 
    $n = isPerfect($i);
    if($n > 0) {
        echo "$n <BR>";
    }
}
function isPerfect($num) {
    $sum = 0;
    for ($j=1; $j < $num; $j++) { 
        if($num % $j == 0) {
            $sum = $sum +  $j;
        }
    }
    if($sum == $num) {
        return $num;
    } 
}
?>
=======================================================
3. Write a PHP Script to display the reverse of a number. E.g. 607 =>706
answer => 
<?php  
    $num = 607;
    $n = 607;  
    $rev = 0;  
    while ($num > 0) {  
        $rem = $num % 10;  
        $rev = ($rev * 10) + $rem;  
        $num = (int)($num / 10);   
        }  
    echo "Reverse number of $n is: $rev <BR>";  
  ?>  
=======================================================
4. Write a PHP Script to display Armstrong numbers between 1 to 500.
answer => 
<?php
    echo "Armstrong numbers between 1 to 500.<BR>";
        for ($i=1; $i < 500; $i++) { 
        $n = isArmstrong($i); // function call
        if($n > 0) {
            echo $n."<BR>";
        }
    }
    function isArmstrong($num) {
        $sum = 0; $rem=0;
        $check = $num;
        while($num > 0) {
            $rem = $num % 10;
            $sum = $sum + $rem * $rem * $rem;
            $num = (int) ($num / 10);
        }
        if($sum == $check) {
            return $check;
        } else {
            return -1;
        }
    }
?>
=======================================================
Set C : 
1. Write a PHP script to display a number in words (Use Switch case)
e.g. 345–three four five
answer => 
<?php
    $num = 345; 
    $rev = 0;
        $check = $num;
        // Reverse number 
        while($num > 0) {
            $rem = $num % 10;
            $rev = ($rev * 10) + $rem;
            $num = (int)($num / 10);
        }
    echo "The number is $check <BR>";
    while($rev > 1) {
        // Reverse the number
        $r = $rev % 10;
        $rev = (int)$rev / 10;
                switch ($r) {
            case 1:
                echo "One ";
                break;
            case 2:
                echo "Two ";
                break;
            case 3:
                echo "Three ";
                break;
            case 4:
                echo "Four ";
                break;
            case 5:
                echo "Five ";
                break;
            case 6:
                echo "Six ";
                break;
            case 7:
                echo "Seven ";
                break;
            case 8:
                echo "Eight ";
                break;
            case 9:
                echo "Nine ";
                break;
            case 0:
                echo "Zero ";
                break;
            default : 
            echo "wrong";
            
        }
    }
   ?>
========================================================
2. Write a PHP script to change the background color of the browser using a switch statement according to the day of the week.
answer => 
<?php
$day = date("l");
switch($day) {
    case 'Monday':
        $bg_color = "red";
        echo "Today's day is $day and that color is $bg_color <BR>";
        break;
    case 'Tuesday':
        $bg_color = "blue";
        echo "Today's day is $day and that color is $bg_color <BR>";
        break;
    case 'Wednesday':
        $bg_color = "blue";
        echo "Today's day is $day and that color is $bg_color <BR>";
        break;
    case 'Thursday':
        $bg_color = "gray";
        echo "Today's day is $day and that color is $bg_color <BR>";
        break;
    case 'Friday':
        $bg_color = "yellow";
        echo "Today's day is $day and that color is $bg_color <BR>";
        break;
    case 'Saturday':
        $bg_color = "green";
        echo "Today's day is $day and that color is $bg_color <BR>";
        break;
    case 'Sunday':
    default:
        $bg_color = "black";
        break;
}
echo "<body style='background-color:$bg_color'></body>";
?>
=======================================================
3. Write a PHP script to count the total number of even and odd numbers between 1 to 1000. 
answer => 
<?php
    echo "Total number of even and odd numbers between 1 to 1000 <BR>";
    $even_cnt = 0;
    $odd_cnt = 0;
    for ($i=1; $i <= 1000; $i++) { 
        if($i % 2 == 0) {
            $even_cnt++;
        } else {
            $odd_cnt++;
        }
    }
    echo "Total Even numbers is $even_cnt <BR>";
    echo "Total Odd numbers is $odd_cnt <BR>";
?>
===========================================================

Post a Comment

0 Comments

Ad Code

Responsive Advertisement