-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheng_calc.php
More file actions
69 lines (63 loc) · 2.51 KB
/
eng_calc.php
File metadata and controls
69 lines (63 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
echo '<h2>Engineering Mathematics Calculator</h2>';
// ── Problem 1: Circle ──
echo '<h3>1. Circle (r = 8.5 cm)</h3>';
$r = 8.5;
$area = M_PI * $r ** 2;
$circ = 2 * M_PI * $r;
echo 'Formula: Area = π × r² | Circumference = 2πr<br>';
echo 'Area: ' . number_format($area, 4) . ' cm²<br>';
echo 'Circumference: ' . number_format($circ, 4) . ' cm<br><br>';
// ── Problem 2: Ohm's Law ──
echo '<h3>2. Ohm\'s Law (V=240V, R=56Ω)</h3>';
$V = 240;
$R = 56;
$current = $V / $R;
$power = ($V ** 2) / $R;
echo 'Formula: I = V/R | P = V²/R<br>';
echo 'Current I: ' . number_format($current, 4) . ' A<br>';
echo 'Power P: ' . number_format($power, 2) . ' W<br><br>';
// ── Problem 3: Pythagoras ──
echo '<h3>3. Pythagoras (a=7, b=24)</h3>';
$a = 7; $b = 24;
$c = sqrt($a**2 + $b**2);
$verify = round($a**2 + $b**2, 4) === round($c**2, 4);
echo 'Formula: c = √(a² + b²)<br>';
echo 'Hypotenuse c: ' . number_format($c, 4) . '<br>';
echo 'Verification a²+b²=c²: ' . ($verify ? 'TRUE' : 'FALSE') . '<br><br>';
// ── Problem 4: Compound Interest ──
echo '<h3>4. Compound Interest</h3>';
$P = 75000;
$r = 0.12;
$n = 12;
$t = 5;
$A = $P * (1 + $r/$n) ** ($n * $t);
$interest = $A - $P;
$monthly = $A / ($t * 12);
echo 'Formula: A = P(1 + r/n)^(nt)<br>';
echo 'Final Amount: KES ' . number_format($A, 2) . '<br>';
echo 'Total Interest: KES ' . number_format($interest, 2) . '<br>';
echo 'Monthly Payment: KES ' . number_format($monthly, 2) . '<br><br>';
// ── Problem 5: Logarithms ──
echo '<h3>5. Logarithms</h3>';
$log2_1024 = log(1024, 2);
$log10_1m = log10(1000000);
$ln_e2 = log(M_E ** 2);
echo 'log₂(1024) = ' . number_format($log2_1024, 4) . ' (expected: 10)<br>';
echo 'log₁₀(1000000) = ' . number_format($log10_1m, 4) . ' (expected: 6)<br>';
echo 'ln(e²) = ' . number_format($ln_e2, 4) . ' (expected: 2)<br><br>';
// ── Bonus: Random Numbers ──
echo '<h3>Bonus: Random Number Analysis</h3>';
$nums = [rand(1,100), rand(1,100), rand(1,100), rand(1,100), rand(1,100)];
echo 'Numbers: ' . implode(', ', $nums) . '<br>';
echo 'Sum: ' . array_sum($nums) . '<br>';
echo 'Mean: ' . number_format(array_sum($nums)/5, 2) . '<br>';
echo 'Primes: ';
foreach ($nums as $n) {
$isPrime = $n > 1;
for ($i = 2; $i <= sqrt($n); $i++) {
if ($n % $i === 0) { $isPrime = false; break; }
}
if ($isPrime) echo $n . ' ';
}
echo '<br>';