Q:

PHP find output programs (Math Functions) | set 2

belongs to collection: PHP Find Output Programs

0

Find the output of PHP programs | Math Functions | Set 2: Enhance the knowledge of PHP Math Functions concepts by solving and finding the output of some PHP programs.

Question 1:

<?php
    $NUM1 = 2.51;
    $NUM2 = 2.49;
    $NUM3 = 3.21;
    
    $RES1 = round($NUM1 + $NUM2 + $NUM3);
    $RES2 = round($NUM1) + round($NUM2) + round($NUM3);
    
    print ($RES1 . ' ' . $RES2);
?>

Question 2:

<?php
    $NUM1 = 10.6;
    $NUM2 = 2.5;
    
    $NUM4 = modf($NUM1, $NUM2);
    
    print ($NUM3 . ' ' . $NUM4);
?>

Question 3:

<?php
    $NUM1 = 10.6;
    $NUM2 = 2.5;
    
    $NUM3 = $NUM1 % $NUM2;
    $NUM4 = fmod($NUM1, $NUM2);
    
    print ($NUM3 . ' ' . $NUM4);
?>

Question 4:

<?php
    echo (round(2.46583, 2) . "<br>");
    echo (round(54321, -3) . "<br>");
    echo (round(4.6, 0, PHP_ROUND_HALF_UP) . "<br>");
    echo (round(5.3, 0, PHP_ROUND_HALF_DOWN) . "<br>");
    echo (round(6.2, 0, PHP_ROUND_HALF_EVEN) . "<br>");
    echo round(9.5, 0, PHP_ROUND_HALF_ODD);
?>

Question 5:

<?php
    $num = PI();
    
    $deg = rad2deg($num);
    
    echo $deg;
?>

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Output:

8 8

Explanation:

The above program will print "8 8" on the webpage. In the above program, we created 3 variables $NUM1, $NUM2, and $NUM initialized with 2.51, 2.49, and 3.21. Here, we used built-in function round().

The round() function is used to round off the floating-point number. For example, if the number is 2.51 then it becomes 3 or if the number is 2.49 then it becomes 2.

Now we evaluate expressions one by one:

$RES1 = round($NUM1+$NUM2+$NUM3);
$RES1 = round(2.51+2.49+3.21);
$RES1 = round(5+3.21);
$RES1 = round(8.21);
$RES1 = 8;

And,

$RES2 = round($NUM1)+round($NUM2)+round($NUM3);
$RES2 = round(2.51)+round(2.49)+round(3.21);
$RES2 = 3 + 2 + 3;
$RES2 = 8;

Then the final values of $RES1 and $RES2 will be printed on the webpage.

Answer  2:

Output:

PHP Fatal error:  Uncaught Error: Call to undefined function modf() 
in /home/main.php:5
Stack trace:
#0 {main}
  thrown in /home/main.php on line 5

Explanation:

The above program will generate syntax error because we used modf() function in the above code, the modf() is not a built-in function then it will generate a syntax error.

Answer  3:

Output:

0 0.6

Explanation:

The above code will print "0 0.6" on the webpage, we cannot get the remainder from floating-point numbers using modulus operator '%'.

To get the remainder from floating-point numbers, we need to use the built-in function fmod().

Then the final values of $NUM3 and $NUM4 will be 0 and 0.6 respectively.

Answer  4:

Output:

2.47
54000
5
5
6
9

Explanation:

In the above program, we used variants of the round() function. The above statements will print the following output:

2.47
54000
5
5
6
9

Here, we used following predefined constants:

PHP_ROUND_HALF_UP
PHP_ROUND_HALF_DOWN
PHP_ROUND_HALF_EVEN	
PHP_ROUND_HALF_ODD

All given constants round off the specified number.

Answer  5:

Output:

180

Explanation:

The above program will print 180 on the webpage. In the above program, we defined a variable $num initialized with PI() value, and we used one more built-in function rad2deg(), which is used to convert the radian value to the degree.

The function rad2deg() will return 180 for PI value 3.1415926535898, and assigned to variable $deg. Finally, we print the value of $deg.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Find Output Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
PHP find output programs (Date and Time) | set 1... >>
<< PHP find output programs (Math Functions) | set 1...