Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

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.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
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
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
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