Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Math Library

The C++ math library is actually C’s math library. It is easy to use and is accessed by including cmath.

#include <cmath>

Square Root

The function sqrt is used to compute the square root of a number. It takes only one argument, the number, whose square root needs to be calculated.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int val = 10;
cout << sqrt(val) << endl;
// prints 3.16228
return 0;
}

Powers

The function pow is used to​ calculate the power of a number in C++. It takes as its first argument the number itself and the value to which it needs to be raised is the second argument.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int val = 5;
// we use the pow function to compute the powers of the integer values given
cout << pow(val, 2) << endl;
// prints 25
cout << pow(val, 3) << endl;
// prints 125
cout << pow(val, 0.5) << endl;
// prints 2.23607
return 0;
}

Trigonometry

To perform trigonometric operations, the cmath library provides the functions sin​, cos and tan. Each of the three takes​ only one argument, the number, on which these operations need to be applied.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
float val = 5.7;

// here the sin, cos and tan functions are being used to compute the trignometric values
cout << sin(val) << endl;
// prints -0.550686
cout << cos(val) << endl;
// prints 0.834713
cout << tan(val) << endl;
// prints -0.659731
return 0;
}

Note: Trigonometric functions in cmath use RADIANS.

Try the following example in the editor below.

You are given two float variables A and B, perform the operations defined in comments in the editor below.

Start solving Math Library on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test