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 ;           ...

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...

) Create the following GUI screen using appropriate layout managers. Accept the name, class , hobbies of the user and apply the changes and display the selected options in a text box.

  import javax.swing.*; import java.awt.*; import java.awt.event.*; class Swing2 extends JFrame implements ActionListener {     JLabel l1 , l2 , l3 ;         JButton b ;         JRadioButton r1 , r2 , r3 ;         JCheckBox c1 , c2 , c3 ;         JTextField t1 , t2 ;         ButtonGroup b1 ;         JPanel p1 , p2 ;     static int cnt ;         private StringBuffer s1 = new StringBuffer ();                 Swing2 ()         {                             b1= new ButtonGroup ();                 p1= new JPanel ();                 p2= new JPanel ();               ...