Skip to main content

1) Write a script to accept two integers(Use html form having 2 textboxes). Write a PHP script to, a. Find mod of the two numbers. b. Find the power of first number raised to the second. c. Find the sum of first n numbers (considering first number as n) d. Find the factorial of second number. (Write separate function for each of the above operations.)

PHP CODE:

<?php
$a=$_GET['num1'];
$b=$_GET['num2'];
$ch=$_GET['op'];
switch($ch)
{
    case 1 : mod($a,$b);
         break;
         
    case 2 : power($a,$b);
         break;
         
    case 3 : sumN($a);
         break;
         
    case 4 : fact($b);
         break;
}

function mod($a,$b)
{
    $c=$a%$b;
    echo "Mod of first and second number : ";
    echo $c;
}

function power($a,$b)
{
    $ans=1;
    $n1=$a;
    while($n1>0)
    {
        $ans=$ans*$b;
        $n1--;
    }
    echo "$b raised to power $a : $ans\n";
}

function sumN($a)
{
    $n2=$a;
    $n=$n2;
    $sum=0;
    while($n>=0)
    {
        $sum=$sum+$n;
        $n--;
    }
    echo "Sum of $n2 numbers is $sum\n";    
}

function fact($b)
{
    $n2=$b;
    $facto=1;
    for($x=$n2;$x>=1;$x--)
    {
        $facto=$facto*$x;
    }
    echo "Factorial of $b is $facto\n";
}
?>

HTML CODE:

 <html>
<body>
<form action="a1.php" method="GET">
    First Number : <input type="number" name="num1"><br>
    Second Number : <input type="number" name="num2"><br>
    <p>Which Operation do you want to perform ? </p>
    <input type="radio" name="op" value="1">
        <label for="mod">Mod of the two numbers</label><br>
    <input type="radio" name="op" value="2">
        <label for="power">Power of first number raised to second number</label><br>
    <input type="radio" name="op" value="3">
        <label for="sum">Sum of first n numbers(considering first number as n)</label>
<br>
    <input type="radio" name="op" value="4">
        <label for="fact">Factorial of second number</label><br>
<input type="submit">
</form>
</body>
</html>


Comments

Popular posts from this blog

Write a program to read book information (bookid, bookname, bookprice, bookqty) in file “book.dat”. Write a menu driven program to perform the following operations using Random access file: i. Search for a specific book by name. ii. Display all book and total cost

  import java . io .*; import java . util .*; class Book {       String name , id ;       int qty ;       double price , total ;       Book ( String i , String n , String p , String q )      {               name = n ;               id = i ;               qty = Integer . parseInt ( q );               price = Double . parseDouble ( p );               total = qty * price ;      }       public String toString ()      {               System . out . println ( "name\t id\t qty\t price\t total" );               String s = name + "\t" + id + "\t" + qty + "\t" + price + "\t" + total ;           ...

Define a class MyDate (day, month, year) with methods to accept and display a MyDate object. Accept date as dd, mm, yyyy. Throw user defined exception “InvalidDateException” if the date is invalid. Examples of invalid dates : 03 15 2019, 31 6 2000, 29 2 2021

  import java . io .*; import java . util .*; class InvalidDateException extends Exception {       InvalidDateException ()       {               System . out . println ( "Invalid Date" );       } } class MyDate {       int day , mon , yr ;       void accept ( int d , int m , int y )       {             day = d ;             mon = m ;             yr = y ;       }       void display ()       {             System . out . println ( "Date is valid : " + day + "/" + mon + "/" + yr );       } } class Date {       public static void main ( String args []) throws Exception       {             Scanner sc = new Sca...

Write a Java program to design a screen using Awt that will take a user name and password. If the user name and password are not same, raise an Exception with appropriate message. User can have 3 login chances only. Use clear button to clear the TextFields.

  import java . awt .*; import java . awt . event .*; import javax . swing .*; class InvalidPasswordException extends Exception {       InvalidPasswordException ()       {             System . out . println ( "Username and password is not same" );       } } public class Password extends Frame implements ActionListener {       Label uname , upass ;       TextField nametext ;       TextField passtext , msg ;       Button login , Clear ;       Panel p ;       int attempt = 0 ;       char c = '*' ;             public void login ()       {             p = new Panel ();             uname = new Label ( "Username : " , Label . CENTER );             upass = n...