0% found this document useful (0 votes)
47 views13 pages

PHP Models

Program 1 finds the factorial of number 6 by initializing a variable $fact to 1 and multiplying it by integers from 1 to 6 in a for loop. Program 2 prints all prime numbers between 1 and 100 by checking if a number is evenly divisible by numbers from 1 to itself using nested for loops. Program 3 checks if number 371 is an Armstrong number by summing the cubes of its digits and comparing it to the original number.

Uploaded by

Akash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views13 pages

PHP Models

Program 1 finds the factorial of number 6 by initializing a variable $fact to 1 and multiplying it by integers from 1 to 6 in a for loop. Program 2 prints all prime numbers between 1 and 100 by checking if a number is evenly divisible by numbers from 1 to itself using nested for loops. Program 3 checks if number 371 is an Armstrong number by summing the cubes of its digits and comparing it to the original number.

Uploaded by

Akash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

PROGRAM NO       : 9

DATE    :

AIM              : Find  factorial of a number.

PROGRAM

<?php 
$n=6; 
$fact=1; 
for($i=1;$i<=$n;$i++) 

$fact=$fact*$i; 

echo "Factorial of ".$n." is ".$fact; 
?>

OUTPUT

Factorial of 6 is 720
PROGRAM NO       : 10
DATE    :

AIM              : Print all prime numbers from 1 to 100.

PROGRAM

<?php 
echo "<br>Prime Numbers between 1 and 100 : <br>"; 
for($i=1;$i<=100;$i++) 

$t=0; 
for($j=1;$j<=$i;$j++) 

if($i%$j==0) 

$t=$t+1; 


if($t ==  2) 

echo "  ".$i." "; 


?>

OUTPUT

Prime Numbers between 1 and 100 : 
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 
PROGRAM NO   : 11
DATE   :

AIM   : check whether number is amstrong or not.

PROGRAM

<?php 
$n=371; 
$temp=$n; 
while($n>0) 

$r=$n%10; 
$s=$s+($r*$r*$r); 
$n=$n/10; 

if($temp==$s) 
echo"$temp is armstrong"; 
else 
echo"$temp is not armstrong"; 
?>

OUTPUT

371 is armstrong
PROGRAM NO  : 12
DATE            :

AIM  : Program to read a students details including name,roll no and 
marks of 5 subjects then display students details including calculated grade and 
percentage of mark scored by the student using OOPS concept.

PROGRAM

<?php 
class student 

public $name,$rno; 
public function accept($n,$r) 

$this­>name=$n; 
$this­>rno=$r; 

public function display() 

$mark=array(50,60,70,75,65); 
$t=0; 
foreach($mark as $value) 
$t=$t+$value; 
$p=($t/500)*100; 

echo"<br>Student Details"; 
echo"<br>­­­­­­­­­­­­­­­­­­­­­­­­­"; 
echo "<br> Name:".$this­>name; 
echo "<br> Roll Number:". $this­>rno; 
echo "<br> Marks Obtained"; 
foreach($mark as $value) 
echo "<br> $value"; 
echo "<br>Total=$t"; 
echo "<br>Percentage=$p%"; 

if ($p >90) 
echo "<br>Grade:A"; 
elseif ($p>=80 && $p<=90) 
echo"<br>Grade:B"; 
elseif ($p>=70 && $p<=80) 
echo"<br>Grade:C"; 
elseif ($p>=60 && $p<=70) 
echo"<br>Grade:D"; 
elseif ($p>=50 && $p<=60) 
echo"<br>Grade:E"; 
else 
echo"<br>Grade:F"; 


$s =new student; 
$s­>accept("Mejo",10); 
$s­>display(); 
?>

OUTPUT

Student Details
­­­­­­­­­­­­­­­­­­­­­­­­­
Name:Mejo
Roll Number:10
Marks Obtained
50
60
70
75
65
Total=320
Percentage=64%
Grade:D
PROGRAM NO : 13
DATE :

AIM : Program to implement working of  constructors and destructors.

PROGRAM

<?php 
class BaseClass 

function __construct() 

echo "BaseClass constructor<br>"; 
}  
function __destruct() 

echo "Destroying SuperClass<br>"; 

class SubClass extends BaseClass 

function __construct() 

parent::__construct(); 
echo "SubClass constructor<br>"; 

function __destruct() 

parent::__destruct(); 
print "Destroying SubClass<br>"; 


$obj = new BaseClass(); 
$obj = new SubClass(); 
?>

OUTPUT

BaseClass constructor
BaseClass constructor
SubClass constructor
Destroying SuperClass
Destroying SuperClass
Destroying SubClass
PROGRAM NO :  14
DATE :

AIM :  Write a program to find the area and perimeter of rectangle 
using  parameterized constructor.

PROGRAM

<?php 
class Rectangle 

function __construct($a,$b) 

$c=$a*$b; 
echo "Area of Rectangle = $c"."<br>"; 
$p=2*($a+$b); 
echo "Perimeter of Rectangle = $p"; 


$obj = new Rectangle(5,2); 
?>

OUTPUT

Area of Rectangle = 10
Perimeter of Rectangle = 14
PROGRAM NO : 15
DATE :

AIM : Find area of circle and rectangle using method overloading.

PROGRAM

<?php
class area
{
public function __call($funname,$args)
{
if($funname==Area)
{
$count=count($args);
switch($count)
{
case "1":
$r=5;
$area=3.14*$r*$r;
echo "Area of Circle = $area";
break;
case "2":
$l=5;
$b=2;
$area=$l*$b;
echo "<br>Area of Rectangle = $area";
break;
default:
echo "Not defined";
}
}
else
{
echo "Function $funname Not Exist";
}
}
}
$obj=new area;
$obj­>Area($r);
$obj­>Area($l,$b);
?>
OUTPUT

Area of Circle = 78.5
Area of Rectangle = 10
PROGRAM NO : 16
DATE :

AIM : Create a program to demonstrate working of static function.

PROGRAM

<?php
class test
{
public static function fun()
{
echo "Hai<br>";
}
public function fun1()
{
echo "Hello";
self :: fun();
}
}
test :: fun1();
$obj=new test();
$obj­>fun1();
?>

OUTPUT

HelloHai
HelloHai
PROGRAM NO : 17
DATE :

AIM : Program to demonstrate interface.

PROGRAM

<?php
interface Addition
{
public function Add($a,$b);
}
interface Substraction extends Addition
{
public function Substract($a,$b);
}
interface Multiplication extends Substraction
{
public function Multiply($a,$b);
}
interface Division extends Multiplication
{
public function Divide($a,$b);
}
class Mathoperations implements Division
{
public function Add($a,$b)
{
$c=$a+$b;
echo "Sum of $a and $b is $c";
}
public function Substract($a,$b)
{
$c=$a­$b;
echo "<br>Difference of $a and $b is $c";
}
public function Multiply($a,$b)
{
$c=$a*$b;
echo "<br>Product of $a and $b is $c";
}
public function Divide($a,$b)
{
$c=$a/$b;
echo "<br>Quotient of $a and $b is $c";
} }
$obj=new Mathoperations;
$obj­>Add(6,2);
$obj­>Substract(6,2);
$obj­>Multiply(6,2);
$obj­>Divide(6,2);
?>

OUTPUT

Sum of 6 and 2 is 8
Difference of 6 and 2 is 4
Product of 6 and 2 is 12
Quotient of 6 and 2 is 3
PROGRAM NO :  18
DATE :

AIM : Write a program to implement autoload classes.

PROGRAM

<?php
class auto
{
public function autos()
{
echo "=============================<br>";
echo "<b><i>AutoLoad Function Program</i></b><br>";
echo “=============================<br>";
}
}
$obj=new auto();
$obj­>autos();

function __autoload($autoload2)
{
include $autoload2.'.php';
}
$obj1=new autoload2();
$obj1­>autoload();
?>

OUTPUT

============================
AutoLoad Function Call Program
============================

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy