Tuesday 16 January 2018

SwitchCase Statement Programs in PHP

Write a  program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer, calculate percentage and grade according to given conditions using switch case
If percentage >= 90% : Grade A
If percentage >= 80% : Grade B
If percentage >= 70% : Grade C
If percentage >= 60% : Grade D
If percentage >= 40% : Grade E
If percentage < 40% : Fail

Perc.html
<html>
<title> Grade </title>
<body>
<h1> Display Grade of Student as per marks obtained</h1>
<form method="POST" action="perc1.php">
Enter the Marks of the student </br>
<fieldset>
Physics:  <input type="number" name="phy"/></br></br>
Chemistry:<input type="number" name="chem"/></br></br>
Maths:    <input type="number" name="maths"/></br></br>
Biology:  <input type="number" name="bio"/></br></br>
English:  <input type="number" name="eng"/></br></br>
<input type="submit" value="Calculate" name="subtn"/>
</fieldset>
</form>
</body>
</html>

perc1.php
<?php
$ph=$_POST['phy'];
$ch=$_POST['chem'];
$mt=$_POST['maths'];
$bi=$_POST['bio'];
$en=$_POST['eng'];
$per=0;
if(isset($_POST['subtn']))
{
$per=($ph+$ch+$mt+$bi+$en)/5;
echo"Percentage obtained by student is $per </br>";

if($per>=90)
{
$per=1;
}
else if($per>=80)
{
$per=2;
}
else if($per>=70)
{
$per=3;
}
else if($per>=60)
{
$per=4;
}
else if($per>=40)
{
$per=5;
}
else
{
$per=6;
}

switch($per)
{
case 1:
echo "Grade : A";
break;

case 2:
echo "Grade : B";
break;

case 3:
echo "Grade : C";
break;

case 4:
echo "Grade : D";
break;

case 5:
echo "Grade : E";
break;

default:
echo "Fail!!";
}

}

?>

Create Simple Calculator(perform all arithmetic operation)using Switch Case

Calc.html
<html>
<body>
<h1> Simple Calculator Example</h1>
<form method="POST" action="Calc1.php">
Enter operator: <input type="text" name="op1"/></br></br>
Enter 1stNumber:<input type="number" name="no1"/></br></br>
Enter 2ndNumber: <input type="number" name="no2"/></br></br>
<input type="submit" value="Calculate" name="subtn"/>
</form>
</body>
</html>

Calc1.php

<?php
$op=$_POST['op1'];
$firstNumber=$_POST['no1'];
$secondNumber=$_POST['no2'];
if(isset($_POST['subtn']))
{
     switch($op)
        {
        case '+':
           echo(($firstNumber + $secondNumber));
            break;
        case '-':
            echo(($firstNumber - $secondNumber));
            break;
         case '*':
           echo(($firstNumber * $secondNumber));
            break;

        case '/':
           echo(($firstNumber / $secondNumber));
            break;

        // operator doesn't match  (+, -, *, /)
        default:
          echo("invalid operator");
    }
}
?>

To check vowel or consonant using switch case

Vow.html
<html>
<body>
<h1> Check vowel and consonant</h1>
<form method="POST" action="Vow1.php">
Enter Character: <input type="text" name="chr"/></br></br>
<input type="submit" value="Calculate" name="subtn"/>
</form>
</body>
</html>

Vow1.php

<?php
$alphabet=$_POST['chr'];
if(isset($_POST['subtn']))
{
switch($alphabet)
    {
        case 'a':
            echo("Vowel");
         break;
        case 'e':
             echo("Vowel");
            break;
        case 'i':
             echo("Vowel");
            break;
        case 'o':
             echo("Vowel");
            break;
        case 'u':
             echo("Vowel");
            break;
        case 'A':
            echo("Vowel");
            break;
        case 'E':
            echo("Vowel");
            break;
        case 'I':
             echo("Vowel");
            break;
        case 'O':
             echo("Vowel");
            break;
        case 'U':
            echo("Vowel");
            break;
    default:
            echo("Consonant");
    }
}
?>

To print number of days in a month using switch case

Mon.html
<html>
<body>
<h1> To print number of days in a month </h1>
<form method="POST" action="Mon1.php">
Enter month(1-12): <input type="text" name="mon"/></br></br>
<input type="submit" value="Calculate" name="subtn"/>
</form>
</body>
</html>

Mon1.php

<?php
$month=$_POST['mon'];
if(isset($_POST['subtn']))
{
switch($month)
    {
        case 1:
            echo("31 days");
            break;
        case 2:
            echo("28/29 days");
            break;
        case 3:
            echo("31 days");
            break;
        case 4:
          echo("30 days");
            break;
        case 5:
            echo("31 days");
            break;
        case 6:
            echo("30 days");
            break;
        case 7:
            echo("31 days");
            break;
        case 8:
           echo("31 days");
            break;
        case 9:
            echo("30 days");
            break;
        case 10:
            echo("31 days");
            break;
        case 11:
           echo("30 days");
            break;
        case 12:
            echo("31 days");
            break;
        default:
           echo("Invalid month! Please enter month number between 1-12");
}
}
?>

To print day of week using switch case

Wee.html
<html>
<body>
<h1> To print day of week </h1>
<form method="POST" action="Wee1.php">
Enter week(1-7): <input type="text" name="wee"/></br></br>
<input type="submit" value="Calculate" name="subtn"/>
</form>
</body>
</html>

Wee1.php
<?php
$week=$_POST['wee'];
if(isset($_POST['subtn']))
{
switch($week)
    {
        case 1:
           echo("Monday");
            break;
        case 2:
            echo("Tuesday");
            break;
        case 3:
            echo("Wednesday");
            break;
        case 4:
             echo("Thursday");
            break;
        case 5:
             echo("Friday");
            break;
        case 6:
             echo("Saturday");
            break;
        case 7:
             echo("Sunday");
            break;
        default:
           echo("Invalid week! Please enter week number between 1-7.");
          }
}
?>

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...