ADVERTISEMENT
ADVERTISEMENT

PHP find output programs (Math Functions) | set 2

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.
Submitted by Nidhi, on January 25, 2021

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);
?>

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.

Question 2:

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

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.

Question 3:

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

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.

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);
?>

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.

Question 5:

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

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.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.