Time: 02:20;
Many times PHP Developers also facing such questions while interviewing.
To get hands-on experience in such questions you should study the Clock Angle Problem concept and formulae.
ANSWER: 50 Degrees.
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 |
<?php // CALCULATE THE ANGLE BETWEEN HOUR HAND AND MINUTE HAND // STEP 1. Set display errors on ini_set('display_errors', 1); // STEP 2. Initialise two variables for hour and minute as below, $h=2; $m=20; $angle=0; // in degrees // STEP 3. Define and call the method for angle calculation function clculateHMAngle($h,$m){ // validate the input if ($h < 0 || $m < 0 || $h >12 || $m > 60) { echo "Please enter corrct input!"; die; } if ($h == 12) $h = 0; if ($m == 60) $m = 0; //Calculate the angle moved by hour hand from the mark of number 12 clockwise $hour_angle = 0.5 * ($h*60 + $m); // 70 degrees //Calculate the angles moved by minute hand from the mark of number 12 clockwise $minute_angle = 6 * $m; // 120 degrees // Calculate the difference between hour hand angle and minute hand angle $angle = abs($hour_angle - $minute_angle); // 70 -120 = abs(-50) = 50 degrees //Get the smallest angle between 360- $angle and $angle $angle = min( (360-$angle), $angle); // 360-50, 50 = 50 Degrees echo $angle; // 50 Degrees } clculateHMAngle($h,$m); ?> |
Hope this helped! 🙂
Please comment if you have any doubt!
Comments