Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
✅ Practice + Quiz  ·  Lesson 19

68 PHP Practice Programs

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

Easy Formula Programs with User Input

Start with these programs first. Every program takes values from the user, applies one clear formula and prints the result. Copy the code, change the input and practise.

1. Add two numbers entered by the user

Reads two numbers and displays their sum.

Formula: Sum = first number + second number
PHP
<?php
echo "Enter first number: ";
$a = (float) trim(fgets(STDIN));
echo "Enter second number: ";
$b = (float) trim(fgets(STDIN));
$result = $a + $b;
echo "Sum = " . $result . PHP_EOL;
Sample Input and Output:
Enter first number: 25
Enter second number: 17
Sum = 42

2. Area of a rectangle

Uses length and breadth to calculate the rectangular area.

Formula: Area = length x breadth
PHP
<?php
echo "Enter length: ";
$length = (float) trim(fgets(STDIN));
echo "Enter breadth: ";
$breadth = (float) trim(fgets(STDIN));
$result = $length * $breadth;
echo "Area = " . $result . PHP_EOL;
Sample Input and Output:
Enter length: 12
Enter breadth: 5
Area = 60

3. Area of a circle

Reads the radius and calculates the area of a circle.

Formula: Area = pi x radius x radius
PHP
<?php
echo "Enter radius: ";
$radius = (float) trim(fgets(STDIN));
$result = 3.14159 * $radius * $radius;
echo "Area = " . $result . PHP_EOL;
Sample Input and Output:
Enter radius: 7
Area = 153.938

4. Metres to kilometres

Converts a distance from metres to kilometres.

Formula: Kilometres = metres / 1000
PHP
<?php
echo "Enter distance in metres: ";
$metres = (float) trim(fgets(STDIN));
$result = $metres / 1000;
echo "Kilometres = " . $result . PHP_EOL;
Sample Input and Output:
Enter distance in metres: 2500
Kilometres = 2.5

5. Kilometres to metres

Converts a distance from kilometres to metres.

Formula: Metres = kilometres x 1000
PHP
<?php
echo "Enter distance in kilometres: ";
$kilometres = (float) trim(fgets(STDIN));
$result = $kilometres * 1000;
echo "Metres = " . $result . PHP_EOL;
Sample Input and Output:
Enter distance in kilometres: 3.5
Metres = 3500

6. Rupees to paise

Converts an amount in rupees into paise.

Formula: Paise = rupees x 100
PHP
<?php
echo "Enter amount in rupees: ";
$rupees = (float) trim(fgets(STDIN));
$result = $rupees * 100;
echo "Paise = " . $result . PHP_EOL;
Sample Input and Output:
Enter amount in rupees: 125.50
Paise = 12550

7. Paise to rupees

Converts an amount in paise into rupees.

Formula: Rupees = paise / 100
PHP
<?php
echo "Enter amount in paise: ";
$paise = (float) trim(fgets(STDIN));
$result = $paise / 100;
echo "Rupees = " . $result . PHP_EOL;
Sample Input and Output:
Enter amount in paise: 8750
Rupees = 87.5

8. Celsius to Fahrenheit

Converts a temperature from Celsius to Fahrenheit.

Formula: F = (C x 9 / 5) + 32
PHP
<?php
echo "Enter temperature in Celsius: ";
$celsius = (float) trim(fgets(STDIN));
$result = ($celsius * 9 / 5) + 32;
echo "Fahrenheit = " . $result . PHP_EOL;
Sample Input and Output:
Enter temperature in Celsius: 30
Fahrenheit = 86

9. Fahrenheit to Celsius

Converts a temperature from Fahrenheit to Celsius.

Formula: C = (F - 32) x 5 / 9
PHP
<?php
echo "Enter temperature in Fahrenheit: ";
$fahrenheit = (float) trim(fgets(STDIN));
$result = ($fahrenheit - 32) * 5 / 9;
echo "Celsius = " . $result . PHP_EOL;
Sample Input and Output:
Enter temperature in Fahrenheit: 98.6
Celsius = 37

10. Simple interest

Calculates simple interest from principal, rate and time.

Formula: SI = principal x rate x time / 100
PHP
<?php
echo "Enter principal: ";
$principal = (float) trim(fgets(STDIN));
echo "Enter annual rate: ";
$rate = (float) trim(fgets(STDIN));
echo "Enter time in years: ";
$time = (float) trim(fgets(STDIN));
$result = $principal * $rate * $time / 100;
echo "Simple interest = " . $result . PHP_EOL;
Sample Input and Output:
Enter principal: 5000
Enter annual rate: 6
Enter time in years: 2
Simple interest = 600

11. Compound interest

Calculates annually compounded interest.

Formula: CI = P(1 + R/100)^T - P
PHP
<?php
echo "Enter principal: ";
$principal = (float) trim(fgets(STDIN));
echo "Enter annual rate: ";
$rate = (float) trim(fgets(STDIN));
echo "Enter time in years: ";
$time = (float) trim(fgets(STDIN));
$result = $principal * pow(1 + $rate / 100, $time) - $principal;
echo "Compound interest = " . $result . PHP_EOL;
Sample Input and Output:
Enter principal: 10000
Enter annual rate: 10
Enter time in years: 2
Compound interest = 2100

12. Area of a triangle

Uses base and height to calculate the triangle area.

Formula: Area = base x height / 2
PHP
<?php
echo "Enter base: ";
$base = (float) trim(fgets(STDIN));
echo "Enter height: ";
$height = (float) trim(fgets(STDIN));
$result = $base * $height / 2;
echo "Area = " . $result . PHP_EOL;
Sample Input and Output:
Enter base: 10
Enter height: 8
Area = 40

13. Perimeter of a rectangle

Adds all four sides of a rectangle.

Formula: Perimeter = 2 x (length + breadth)
PHP
<?php
echo "Enter length: ";
$length = (float) trim(fgets(STDIN));
echo "Enter breadth: ";
$breadth = (float) trim(fgets(STDIN));
$result = 2 * ($length + $breadth);
echo "Perimeter = " . $result . PHP_EOL;
Sample Input and Output:
Enter length: 12
Enter breadth: 5
Perimeter = 34

14. Circumference of a circle

Calculates the distance around a circle from its radius.

Formula: Circumference = 2 x pi x radius
PHP
<?php
echo "Enter radius: ";
$radius = (float) trim(fgets(STDIN));
$result = 2 * 3.14159 * $radius;
echo "Circumference = " . $result . PHP_EOL;
Sample Input and Output:
Enter radius: 7
Circumference = 43.9823

15. Total and percentage of five subjects

Reads five marks, then calculates total and percentage.

Formula: Percentage = total marks / 5
PHP
<?php
echo "Enter marks in subject 1: ";
$m1 = (float) trim(fgets(STDIN));
echo "Enter marks in subject 2: ";
$m2 = (float) trim(fgets(STDIN));
echo "Enter marks in subject 3: ";
$m3 = (float) trim(fgets(STDIN));
echo "Enter marks in subject 4: ";
$m4 = (float) trim(fgets(STDIN));
echo "Enter marks in subject 5: ";
$m5 = (float) trim(fgets(STDIN));
$result = ($m1 + $m2 + $m3 + $m4 + $m5) / 5;
echo "Total = " . ($m1+$m2+$m3+$m4+$m5) . PHP_EOL;
echo "Percentage = " . $result . PHP_EOL;
Sample Input and Output:
Marks: 80 75 90 85 70
Total = 400
Percentage = 80

16. GST amount

Calculates GST and the final bill amount.

Formula: GST = amount x rate / 100
PHP
<?php
echo "Enter amount: ";
$amount = (float) trim(fgets(STDIN));
echo "Enter GST rate: ";
$gstRate = (float) trim(fgets(STDIN));
$result = $amount * $gstRate / 100;
echo "GST = " . $result . PHP_EOL;
echo "Final amount = " . ($amount+$result) . PHP_EOL;
Sample Input and Output:
Enter amount: 2000
Enter GST rate: 18
GST = 360
Final amount = 2360

17. Body mass index (BMI)

Calculates BMI from weight in kilograms and height in metres.

Formula: BMI = weight / (height x height)
PHP
<?php
echo "Enter weight in kg: ";
$weight = (float) trim(fgets(STDIN));
echo "Enter height in metres: ";
$height = (float) trim(fgets(STDIN));
$result = $weight / ($height * $height);
echo "BMI = " . $result . PHP_EOL;
Sample Input and Output:
Enter weight in kg: 72
Enter height in metres: 1.8
BMI = 22.2222

18. Square and cube of a number

Reads one number and prints both its square and cube.

Formula: Square = n x n; Cube = n x n x n
PHP
<?php
echo "Enter a number: ";
$number = (float) trim(fgets(STDIN));
$result = $number * $number;
echo "Square = " . $result . PHP_EOL;
echo "Cube = " . ($number*$number*$number) . PHP_EOL;
Sample Input and Output:
Enter a number: 5
Square = 25
Cube = 125

About

A collection of 50 important PHP practice programs covering calculations, decisions, loops, patterns and arrays. Each is a complete script with expected output.

Basic Calculation Programs

1. Add two numbers
<?php
$a=5; $b=3;
echo "Sum = " . ($a+$b);  // 8
?>
2. Subtract two numbers
<?php
$a=10; $b=4;
echo "Diff = " . ($a-$b);  // 6
?>
3. Multiply two numbers
<?php
$a=6; $b=7;
echo "Product = " . ($a*$b);  // 42
?>
4. Divide two numbers
<?php
$a=10; $b=4;
echo "Result = " . ($a/$b);  // 2.5
?>
5. Find remainder
<?php
$a=17; $b=5;
echo "Remainder = " . ($a%$b);  // 2
?>
6. Area of rectangle
<?php
$l=8; $w=3;
echo "Area = " . ($l*$w);  // 24
?>
7. Area of circle
<?php
$r=7;
echo "Area = " . (3.14*$r*$r);  // 153.86
?>
8. Simple interest
<?php
$p=1000; $r=5; $t=2;
echo "SI = " . (($p*$r*$t)/100);  // 100
?>
9. Average of three numbers
<?php
$a=10; $b=20; $c=30;
echo "Avg = " . (($a+$b+$c)/3);  // 20
?>
10. Swap two numbers
<?php
$a=5; $b=9;
[$a,$b]=[$b,$a];
echo "a=$a b=$b";  // a=9 b=5
?>

If-Else and If-Elseif Programs

1. Check even or odd
<?php
$n=7;
if($n%2==0) echo "Even"; else echo "Odd";  // Odd
?>
2. Largest of two numbers
<?php
$a=12; $b=20;
echo ($a>$b)?$a:$b;  // 20
?>
3. Largest of three numbers
<?php
$a=5; $b=9; $c=3;
if($a>=$b && $a>=$c) echo $a;
elseif($b>=$c) echo $b;
else echo $c;  // 9
?>
4. Positive, negative or zero
<?php
$n=-4;
if($n>0) echo "Positive";
elseif($n<0) echo "Negative";
else echo "Zero";  // Negative
?>
5. Check pass or fail
<?php
$marks=45;
if($marks>=33) echo "Pass"; else echo "Fail";  // Pass
?>
6. Grade using if-elseif
<?php
$m=82;
if($m>=90) echo "A+";
elseif($m>=75) echo "A";
elseif($m>=33) echo "Pass";
else echo "Fail";  // A
?>
7. Check leap year
<?php
$y=2024;
if(($y%4==0 && $y%100!=0)||$y%400==0) echo "Leap";
else echo "Not Leap";  // Leap
?>
8. Check vowel or consonant
<?php
$ch="e";
if(in_array($ch,["a","e","i","o","u"])) echo "Vowel";
else echo "Consonant";  // Vowel
?>
9. Divisible by 5 and 11
<?php
$n=55;
if($n%5==0 && $n%11==0) echo "Yes"; else echo "No";  // Yes
?>
10. Largest using ternary
<?php
$a=7; $b=4;
echo ($a>$b)?$a:$b;  // 7
?>

Loop Programs

1. Print 1 to 10
<?php
for($i=1;$i<=10;$i++) echo "$i ";  // 1..10
?>
2. Sum of first N numbers
<?php
$n=5; $sum=0;
for($i=1;$i<=$n;$i++) $sum+=$i;
echo "Sum = $sum";  // 15
?>
3. Factorial
<?php
$n=5; $f=1;
for($i=1;$i<=$n;$i++) $f*=$i;
echo "Factorial = $f";  // 120
?>
4. Multiplication table
<?php
$n=5;
for($i=1;$i<=10;$i++) echo "$n x $i = " . ($n*$i) . "\n";
?>
5. Print even numbers 1-20
<?php
for($i=2;$i<=20;$i+=2) echo "$i ";  // 2..20
?>
6. Reverse a number
<?php
$n=1234; $rev=0;
while($n>0){ $rev=$rev*10+$n%10; $n=intdiv($n,10); }
echo $rev;  // 4321
?>
7. Sum of digits
<?php
$n=123; $s=0;
while($n>0){ $s+=$n%10; $n=intdiv($n,10); }
echo "Sum = $s";  // 6
?>
8. Count digits
<?php
$n=98765; $c=0;
while($n>0){ $c++; $n=intdiv($n,10); }
echo "Digits = $c";  // 5
?>
9. Check prime
<?php
$n=13; $p=true;
for($i=2;$i<$n;$i++) if($n%$i==0) $p=false;
echo $p?"Prime":"Not Prime";  // Prime
?>
10. Fibonacci series
<?php
$a=0; $b=1;
for($i=0;$i<7;$i++){ echo "$a "; $c=$a+$b; $a=$b; $b=$c; }  // 0 1 1 2 3 5 8
?>

Pattern Programs

1. Square of stars
<?php
for($i=1;$i<=4;$i++){ for($j=1;$j<=4;$j++) echo "* "; echo "\n"; }
?>
2. Right triangle
<?php
for($i=1;$i<=5;$i++){ for($j=1;$j<=$i;$j++) echo "* "; echo "\n"; }
?>
3. Inverted triangle
<?php
for($i=5;$i>=1;$i--){ for($j=1;$j<=$i;$j++) echo "* "; echo "\n"; }
?>
4. Number triangle
<?php
for($i=1;$i<=5;$i++){ for($j=1;$j<=$i;$j++) echo "$j "; echo "\n"; }
?>
5. Repeated number triangle
<?php
for($i=1;$i<=5;$i++){ for($j=1;$j<=$i;$j++) echo "$i "; echo "\n"; }
?>
6. Right-aligned triangle
<?php
for($i=1;$i<=4;$i++){ for($j=$i;$j<4;$j++) echo "  "; for($k=1;$k<=$i;$k++) echo "* "; echo "\n"; }
?>
7. Pyramid of stars
<?php
$n=4;
for($i=1;$i<=$n;$i++){ for($j=$i;$j<$n;$j++) echo " "; for($k=1;$k<=2*$i-1;$k++) echo "*"; echo "\n"; }
?>
8. Alphabet triangle
<?php
for($i=1;$i<=5;$i++){ for($j=0;$j<$i;$j++) echo chr(65+$j)." "; echo "\n"; }
?>
9. Diamond pattern
<?php
$n=3;
for($i=1;$i<=$n;$i++){ echo str_repeat(" ",$n-$i).str_repeat("*",2*$i-1)."\n"; }
for($i=$n-1;$i>=1;$i--){ echo str_repeat(" ",$n-$i).str_repeat("*",2*$i-1)."\n"; }
?>
10. Hollow square
<?php
$n=4;
for($i=1;$i<=$n;$i++){ for($j=1;$j<=$n;$j++){ echo ($i==1||$i==$n||$j==1||$j==$n)?"* ":"  "; } echo "\n"; }
?>

Array Programs

1. Sum of array
<?php
$a=[10,20,30,40];
echo "Sum = " . array_sum($a);  // 100
?>
2. Largest in array
<?php
$a=[3,7,2,9,4];
echo "Max = " . max($a);  // 9
?>
3. Smallest in array
<?php
$a=[3,7,2,9,4];
echo "Min = " . min($a);  // 2
?>
4. Average of array
<?php
$a=[10,20,30];
echo "Avg = " . (array_sum($a)/count($a));  // 20
?>
5. Reverse array
<?php
$a=[1,2,3,4,5];
print_r(array_reverse($a));  // 5 4 3 2 1
?>
6. Count even numbers
<?php
$a=[1,2,3,4,5,6]; $c=0;
foreach($a as $x) if($x%2==0) $c++;
echo "Even = $c";  // 3
?>
7. Linear search
<?php
$a=[5,8,12,3]; $key=12;
echo "Found at " . array_search($key,$a);  // 2
?>
8. Copy array
<?php
$a=[1,2,3];
$b=$a;
print_r($b);  // [1,2,3]
?>
9. Sort ascending
<?php
$a=[5,2,8,1];
sort($a);
print_r($a);  // [1,2,5,8]
?>
10. Count positive/negative
<?php
$a=[-1,2,-3,4,5]; $p=0; $n=0;
foreach($a as $x){ if($x>=0) $p++; else $n++; }
echo "Pos=$p Neg=$n";  // Pos=3 Neg=2
?>

User Input वाले आसान Formula Programs

इन programs को सबसे पहले करें। हर program user से value लेता है, एक सीधा formula लगाता है और result दिखाता है। Code को copy करके input बदलकर अभ्यास करें।

1. User द्वारा दिए गए दो numbers जोड़ें

दो numbers input लेकर उनका sum दिखाता है।

Formula: Sum = first number + second number
PHP
<?php
echo "Enter first number: ";
$a = (float) trim(fgets(STDIN));
echo "Enter second number: ";
$b = (float) trim(fgets(STDIN));
$result = $a + $b;
echo "Sum = " . $result . PHP_EOL;
Sample Input और Output:
Enter first number: 25
Enter second number: 17
Sum = 42

2. Rectangle का area

Length और breadth से rectangle का area निकालता है।

Formula: Area = length x breadth
PHP
<?php
echo "Enter length: ";
$length = (float) trim(fgets(STDIN));
echo "Enter breadth: ";
$breadth = (float) trim(fgets(STDIN));
$result = $length * $breadth;
echo "Area = " . $result . PHP_EOL;
Sample Input और Output:
Enter length: 12
Enter breadth: 5
Area = 60

3. Circle का area

Radius input लेकर circle का area निकालता है।

Formula: Area = pi x radius x radius
PHP
<?php
echo "Enter radius: ";
$radius = (float) trim(fgets(STDIN));
$result = 3.14159 * $radius * $radius;
echo "Area = " . $result . PHP_EOL;
Sample Input और Output:
Enter radius: 7
Area = 153.938

4. Metres को kilometres में बदलें

Distance को metres से kilometres में बदलता है।

Formula: Kilometres = metres / 1000
PHP
<?php
echo "Enter distance in metres: ";
$metres = (float) trim(fgets(STDIN));
$result = $metres / 1000;
echo "Kilometres = " . $result . PHP_EOL;
Sample Input और Output:
Enter distance in metres: 2500
Kilometres = 2.5

5. Kilometres को metres में बदलें

Distance को kilometres से metres में बदलता है।

Formula: Metres = kilometres x 1000
PHP
<?php
echo "Enter distance in kilometres: ";
$kilometres = (float) trim(fgets(STDIN));
$result = $kilometres * 1000;
echo "Metres = " . $result . PHP_EOL;
Sample Input और Output:
Enter distance in kilometres: 3.5
Metres = 3500

6. Rupees को paise में बदलें

Rupees की amount को paise में बदलता है।

Formula: Paise = rupees x 100
PHP
<?php
echo "Enter amount in rupees: ";
$rupees = (float) trim(fgets(STDIN));
$result = $rupees * 100;
echo "Paise = " . $result . PHP_EOL;
Sample Input और Output:
Enter amount in rupees: 125.50
Paise = 12550

7. Paise को rupees में बदलें

Paise की amount को rupees में बदलता है।

Formula: Rupees = paise / 100
PHP
<?php
echo "Enter amount in paise: ";
$paise = (float) trim(fgets(STDIN));
$result = $paise / 100;
echo "Rupees = " . $result . PHP_EOL;
Sample Input और Output:
Enter amount in paise: 8750
Rupees = 87.5

8. Celsius को Fahrenheit में बदलें

Temperature को Celsius से Fahrenheit में बदलता है।

Formula: F = (C x 9 / 5) + 32
PHP
<?php
echo "Enter temperature in Celsius: ";
$celsius = (float) trim(fgets(STDIN));
$result = ($celsius * 9 / 5) + 32;
echo "Fahrenheit = " . $result . PHP_EOL;
Sample Input और Output:
Enter temperature in Celsius: 30
Fahrenheit = 86

9. Fahrenheit को Celsius में बदलें

Temperature को Fahrenheit से Celsius में बदलता है।

Formula: C = (F - 32) x 5 / 9
PHP
<?php
echo "Enter temperature in Fahrenheit: ";
$fahrenheit = (float) trim(fgets(STDIN));
$result = ($fahrenheit - 32) * 5 / 9;
echo "Celsius = " . $result . PHP_EOL;
Sample Input और Output:
Enter temperature in Fahrenheit: 98.6
Celsius = 37

10. Simple interest निकालें

Principal, rate और time से simple interest निकालता है।

Formula: SI = principal x rate x time / 100
PHP
<?php
echo "Enter principal: ";
$principal = (float) trim(fgets(STDIN));
echo "Enter annual rate: ";
$rate = (float) trim(fgets(STDIN));
echo "Enter time in years: ";
$time = (float) trim(fgets(STDIN));
$result = $principal * $rate * $time / 100;
echo "Simple interest = " . $result . PHP_EOL;
Sample Input और Output:
Enter principal: 5000
Enter annual rate: 6
Enter time in years: 2
Simple interest = 600

11. Compound interest निकालें

Annual compounding के अनुसार compound interest निकालता है।

Formula: CI = P(1 + R/100)^T - P
PHP
<?php
echo "Enter principal: ";
$principal = (float) trim(fgets(STDIN));
echo "Enter annual rate: ";
$rate = (float) trim(fgets(STDIN));
echo "Enter time in years: ";
$time = (float) trim(fgets(STDIN));
$result = $principal * pow(1 + $rate / 100, $time) - $principal;
echo "Compound interest = " . $result . PHP_EOL;
Sample Input और Output:
Enter principal: 10000
Enter annual rate: 10
Enter time in years: 2
Compound interest = 2100

12. Triangle का area

Base और height से triangle का area निकालता है।

Formula: Area = base x height / 2
PHP
<?php
echo "Enter base: ";
$base = (float) trim(fgets(STDIN));
echo "Enter height: ";
$height = (float) trim(fgets(STDIN));
$result = $base * $height / 2;
echo "Area = " . $result . PHP_EOL;
Sample Input और Output:
Enter base: 10
Enter height: 8
Area = 40

13. Rectangle का perimeter

Rectangle की चारों sides का total निकालता है।

Formula: Perimeter = 2 x (length + breadth)
PHP
<?php
echo "Enter length: ";
$length = (float) trim(fgets(STDIN));
echo "Enter breadth: ";
$breadth = (float) trim(fgets(STDIN));
$result = 2 * ($length + $breadth);
echo "Perimeter = " . $result . PHP_EOL;
Sample Input और Output:
Enter length: 12
Enter breadth: 5
Perimeter = 34

14. Circle की circumference

Radius से circle के चारों ओर की दूरी निकालता है।

Formula: Circumference = 2 x pi x radius
PHP
<?php
echo "Enter radius: ";
$radius = (float) trim(fgets(STDIN));
$result = 2 * 3.14159 * $radius;
echo "Circumference = " . $result . PHP_EOL;
Sample Input और Output:
Enter radius: 7
Circumference = 43.9823

15. Five subjects का total और percentage

पाँच subjects के marks लेकर total और percentage निकालता है।

Formula: Percentage = total marks / 5
PHP
<?php
echo "Enter marks in subject 1: ";
$m1 = (float) trim(fgets(STDIN));
echo "Enter marks in subject 2: ";
$m2 = (float) trim(fgets(STDIN));
echo "Enter marks in subject 3: ";
$m3 = (float) trim(fgets(STDIN));
echo "Enter marks in subject 4: ";
$m4 = (float) trim(fgets(STDIN));
echo "Enter marks in subject 5: ";
$m5 = (float) trim(fgets(STDIN));
$result = ($m1 + $m2 + $m3 + $m4 + $m5) / 5;
echo "Total = " . ($m1+$m2+$m3+$m4+$m5) . PHP_EOL;
echo "Percentage = " . $result . PHP_EOL;
Sample Input और Output:
Marks: 80 75 90 85 70
Total = 400
Percentage = 80

16. GST amount निकालें

Amount और GST rate से tax तथा final bill निकालता है।

Formula: GST = amount x rate / 100
PHP
<?php
echo "Enter amount: ";
$amount = (float) trim(fgets(STDIN));
echo "Enter GST rate: ";
$gstRate = (float) trim(fgets(STDIN));
$result = $amount * $gstRate / 100;
echo "GST = " . $result . PHP_EOL;
echo "Final amount = " . ($amount+$result) . PHP_EOL;
Sample Input और Output:
Enter amount: 2000
Enter GST rate: 18
GST = 360
Final amount = 2360

17. Body mass index (BMI)

Weight (kg) और height (metres) से BMI निकालता है।

Formula: BMI = weight / (height x height)
PHP
<?php
echo "Enter weight in kg: ";
$weight = (float) trim(fgets(STDIN));
echo "Enter height in metres: ";
$height = (float) trim(fgets(STDIN));
$result = $weight / ($height * $height);
echo "BMI = " . $result . PHP_EOL;
Sample Input और Output:
Enter weight in kg: 72
Enter height in metres: 1.8
BMI = 22.2222

18. Number का square और cube

एक number input लेकर उसका square और cube दिखाता है।

Formula: Square = n x n; Cube = n x n x n
PHP
<?php
echo "Enter a number: ";
$number = (float) trim(fgets(STDIN));
$result = $number * $number;
echo "Square = " . $result . PHP_EOL;
echo "Cube = " . ($number*$number*$number) . PHP_EOL;
Sample Input और Output:
Enter a number: 5
Square = 25
Cube = 125

परिचय

50 ज़रूरी PHP practice programs — calculations, decisions, loops, patterns और arrays। हर एक पूरा script है expected output के साथ।

Basic Calculation Programs (गणना)

1. Add two numbers
<?php
$a=5; $b=3;
echo "Sum = " . ($a+$b);  // 8
?>
2. Subtract two numbers
<?php
$a=10; $b=4;
echo "Diff = " . ($a-$b);  // 6
?>
3. Multiply two numbers
<?php
$a=6; $b=7;
echo "Product = " . ($a*$b);  // 42
?>
4. Divide two numbers
<?php
$a=10; $b=4;
echo "Result = " . ($a/$b);  // 2.5
?>
5. Find remainder
<?php
$a=17; $b=5;
echo "Remainder = " . ($a%$b);  // 2
?>
6. Area of rectangle
<?php
$l=8; $w=3;
echo "Area = " . ($l*$w);  // 24
?>
7. Area of circle
<?php
$r=7;
echo "Area = " . (3.14*$r*$r);  // 153.86
?>
8. Simple interest
<?php
$p=1000; $r=5; $t=2;
echo "SI = " . (($p*$r*$t)/100);  // 100
?>
9. Average of three numbers
<?php
$a=10; $b=20; $c=30;
echo "Avg = " . (($a+$b+$c)/3);  // 20
?>
10. Swap two numbers
<?php
$a=5; $b=9;
[$a,$b]=[$b,$a];
echo "a=$a b=$b";  // a=9 b=5
?>

If-Else और If-Elseif Programs

1. Check even or odd
<?php
$n=7;
if($n%2==0) echo "Even"; else echo "Odd";  // Odd
?>
2. Largest of two numbers
<?php
$a=12; $b=20;
echo ($a>$b)?$a:$b;  // 20
?>
3. Largest of three numbers
<?php
$a=5; $b=9; $c=3;
if($a>=$b && $a>=$c) echo $a;
elseif($b>=$c) echo $b;
else echo $c;  // 9
?>
4. Positive, negative or zero
<?php
$n=-4;
if($n>0) echo "Positive";
elseif($n<0) echo "Negative";
else echo "Zero";  // Negative
?>
5. Check pass or fail
<?php
$marks=45;
if($marks>=33) echo "Pass"; else echo "Fail";  // Pass
?>
6. Grade using if-elseif
<?php
$m=82;
if($m>=90) echo "A+";
elseif($m>=75) echo "A";
elseif($m>=33) echo "Pass";
else echo "Fail";  // A
?>
7. Check leap year
<?php
$y=2024;
if(($y%4==0 && $y%100!=0)||$y%400==0) echo "Leap";
else echo "Not Leap";  // Leap
?>
8. Check vowel or consonant
<?php
$ch="e";
if(in_array($ch,["a","e","i","o","u"])) echo "Vowel";
else echo "Consonant";  // Vowel
?>
9. Divisible by 5 and 11
<?php
$n=55;
if($n%5==0 && $n%11==0) echo "Yes"; else echo "No";  // Yes
?>
10. Largest using ternary
<?php
$a=7; $b=4;
echo ($a>$b)?$a:$b;  // 7
?>

Loop Programs (for/while)

1. Print 1 to 10
<?php
for($i=1;$i<=10;$i++) echo "$i ";  // 1..10
?>
2. Sum of first N numbers
<?php
$n=5; $sum=0;
for($i=1;$i<=$n;$i++) $sum+=$i;
echo "Sum = $sum";  // 15
?>
3. Factorial
<?php
$n=5; $f=1;
for($i=1;$i<=$n;$i++) $f*=$i;
echo "Factorial = $f";  // 120
?>
4. Multiplication table
<?php
$n=5;
for($i=1;$i<=10;$i++) echo "$n x $i = " . ($n*$i) . "\n";
?>
5. Print even numbers 1-20
<?php
for($i=2;$i<=20;$i+=2) echo "$i ";  // 2..20
?>
6. Reverse a number
<?php
$n=1234; $rev=0;
while($n>0){ $rev=$rev*10+$n%10; $n=intdiv($n,10); }
echo $rev;  // 4321
?>
7. Sum of digits
<?php
$n=123; $s=0;
while($n>0){ $s+=$n%10; $n=intdiv($n,10); }
echo "Sum = $s";  // 6
?>
8. Count digits
<?php
$n=98765; $c=0;
while($n>0){ $c++; $n=intdiv($n,10); }
echo "Digits = $c";  // 5
?>
9. Check prime
<?php
$n=13; $p=true;
for($i=2;$i<$n;$i++) if($n%$i==0) $p=false;
echo $p?"Prime":"Not Prime";  // Prime
?>
10. Fibonacci series
<?php
$a=0; $b=1;
for($i=0;$i<7;$i++){ echo "$a "; $c=$a+$b; $a=$b; $b=$c; }  // 0 1 1 2 3 5 8
?>

Pattern Programs (आकृति)

1. Square of stars
<?php
for($i=1;$i<=4;$i++){ for($j=1;$j<=4;$j++) echo "* "; echo "\n"; }
?>
2. Right triangle
<?php
for($i=1;$i<=5;$i++){ for($j=1;$j<=$i;$j++) echo "* "; echo "\n"; }
?>
3. Inverted triangle
<?php
for($i=5;$i>=1;$i--){ for($j=1;$j<=$i;$j++) echo "* "; echo "\n"; }
?>
4. Number triangle
<?php
for($i=1;$i<=5;$i++){ for($j=1;$j<=$i;$j++) echo "$j "; echo "\n"; }
?>
5. Repeated number triangle
<?php
for($i=1;$i<=5;$i++){ for($j=1;$j<=$i;$j++) echo "$i "; echo "\n"; }
?>
6. Right-aligned triangle
<?php
for($i=1;$i<=4;$i++){ for($j=$i;$j<4;$j++) echo "  "; for($k=1;$k<=$i;$k++) echo "* "; echo "\n"; }
?>
7. Pyramid of stars
<?php
$n=4;
for($i=1;$i<=$n;$i++){ for($j=$i;$j<$n;$j++) echo " "; for($k=1;$k<=2*$i-1;$k++) echo "*"; echo "\n"; }
?>
8. Alphabet triangle
<?php
for($i=1;$i<=5;$i++){ for($j=0;$j<$i;$j++) echo chr(65+$j)." "; echo "\n"; }
?>
9. Diamond pattern
<?php
$n=3;
for($i=1;$i<=$n;$i++){ echo str_repeat(" ",$n-$i).str_repeat("*",2*$i-1)."\n"; }
for($i=$n-1;$i>=1;$i--){ echo str_repeat(" ",$n-$i).str_repeat("*",2*$i-1)."\n"; }
?>
10. Hollow square
<?php
$n=4;
for($i=1;$i<=$n;$i++){ for($j=1;$j<=$n;$j++){ echo ($i==1||$i==$n||$j==1||$j==$n)?"* ":"  "; } echo "\n"; }
?>

Array Programs (सरणी)

1. Sum of array
<?php
$a=[10,20,30,40];
echo "Sum = " . array_sum($a);  // 100
?>
2. Largest in array
<?php
$a=[3,7,2,9,4];
echo "Max = " . max($a);  // 9
?>
3. Smallest in array
<?php
$a=[3,7,2,9,4];
echo "Min = " . min($a);  // 2
?>
4. Average of array
<?php
$a=[10,20,30];
echo "Avg = " . (array_sum($a)/count($a));  // 20
?>
5. Reverse array
<?php
$a=[1,2,3,4,5];
print_r(array_reverse($a));  // 5 4 3 2 1
?>
6. Count even numbers
<?php
$a=[1,2,3,4,5,6]; $c=0;
foreach($a as $x) if($x%2==0) $c++;
echo "Even = $c";  // 3
?>
7. Linear search
<?php
$a=[5,8,12,3]; $key=12;
echo "Found at " . array_search($key,$a);  // 2
?>
8. Copy array
<?php
$a=[1,2,3];
$b=$a;
print_r($b);  // [1,2,3]
?>
9. Sort ascending
<?php
$a=[5,2,8,1];
sort($a);
print_r($a);  // [1,2,5,8]
?>
10. Count positive/negative
<?php
$a=[-1,2,-3,4,5]; $p=0; $n=0;
foreach($a as $x){ if($x>=0) $p++; else $n++; }
echo "Pos=$p Neg=$n";  // Pos=3 Neg=2
?>
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.