0% found this document useful (0 votes)
54 views

C++ Math: Cosine

This document discusses various mathematical functions in C++ that are included in the math or cmath library. It provides examples of how to use functions like sin, cos, tan, pow, sqrt, floor, ceil, log, and abs. It also covers basic string operations in C++ like concatenation, length, and accessing characters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

C++ Math: Cosine

This document discusses various mathematical functions in C++ that are included in the math or cmath library. It provides examples of how to use functions like sin, cos, tan, pow, sqrt, floor, ceil, log, and abs. It also covers basic string operations in C++ like concatenation, length, and accessing characters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C++ MATH

Mathematical calculations can be done in C++ • The tangent or tan method is used to calculate the tan
programming language using the mathematical functions of the angle given as an argument in degrees. This
which are included in math or cmath library. These function accepts one double integer as an argument and
mathematical functions are defined to do complex returns a double integer which is the value of tan(xo) =
mathematical calculations. sin(x°)∕cos(x°).
Example: • double tan(double)
#include <iostream> Calling syntax
#include <math.h> double x = tan(23.4);
using namespace std; Example:
int main(){ #include <iostream>
double x = 45.3; #include <math.h>
cout << "sin ( "<<x<<" ) = " << sin(x) << endl; using namespace std;
} int main(){

COSINE double x = 45.3;

• The cosine or cos method is used to calculate the cos of cout << "tan ( "<<x<<" ) = " << tan(x) << endl;
the angle given as an argument in degrees. This function }
accepts one double integer as an argument and returns a
double integer which is the value of cos(x°). Output:

• double cos(double) tan( 45.3 ) = 3.86638

Calling syntax POWER


double x = cos(23.4); • The pow function is used to calculate the power of the
base raised to the power of exponent. It accepts two
Example:
double values as arguments which are the base and
#include <iostream> exponents numbers and it returns a single double integer
which is base to the power of exponent.
#include <math.h>
• double pow( double, double)
using namespace std;
Calling syntax
int main(){
double x = pow(2, 4)
double x = 45.3;
cout << "cos ( "<<x<<" ) = " << cos(x) << endl;
Example:
}
#include <iostream>
#include <math.h>
Output:
using namespace std;
cos(45.3 ) = 0.2504
int main(){
TANGENT
double base = 2, power = 4;
cout << "pow( "<<base<<" , "<<power<<" ) = " << int main(){
pow(base, power) << endl;
double a =1.35 ;
}
cout << "sqrt( "<<a<<" ) = " << sqrt(a) << endl;
Output:
}
pow( 2 , 4 ) = 16
Output:
SQRT (SQUARE ROOT) sqrt( 1.35 ) = 0.300105
• sqrt function in C++ returns the square root of the
double integer inside the parameter list. The method
FLOOR
accept a double integer value as input find square root • The floor function is used to return the floor value of
and returns a double integer as output. the given double integer. floor value means rounded
down value. the function accepts a double value as input
• double sqrt(double)
and Returns double integer value calculated using
Calling syntax floor().
double x = sqrt(25.00) • double floor( double)
Example: Calling Syntax
#include <iostream> double x = floor(5.24)
#include <math.h> Example:
using namespace std; #include <iostream>
int main(){ #include <math.h>
double a =25 ; using namespace std;
cout << "sqrt( "<<a<<" ) = " << sqrt(a) << endl; int main(){
} double a =6.24 ;
Output: cout << "floor( "<<a<<" ) = " << floor(a) << endl;
sqrt( 25 ) = 5.00 }

LOG
 The lock function is used to find the natural log of Output:
the given number. This method accepts single floor( 6.24 ) = 6
double integer value finds logarithmic value and
returns a double integer result of log(). CEIL
 double log( double) • The ceil function is used to return the ceil value of the
given double integer. ceil value means rounded up value.
Calling Syntax the function accepts a double value as input and Returns
double integer value calculated using ceil().
double x = log(1.35)
• double ceil( double)
Example:
Calling Syntax
#include <iostream>
double x = ceil(5.24)
#include <math.h>
Example:
using namespace std;
#include <iostream> =abs( -345 ) = 345
#include <math.h>
using namespace std;
int main(){
double a =6.64 ;
cout << "ceil( "<<a<<" ) = " << ceil(a) << endl;
}
Output:
ceil( 6.64 ) = 7

ABS
• The abs function returns the absolute value of the
integer value. The
function accepts an integer value and returns an integer
value that
has the same magnitude but positive sign.
• double abs( double)
Calling Syntax
double x = abs(-512)
Example:
#include <iostream>
#include <math.h>
using namespace std;
int main(){
int a = -345 ;
cout << "abs( "<<a<<" ) = " << abs(a) << endl;
}
Output:

C++
C++ Strings Example:

• Strings are used for storing text. Create a variable of type string and assign it a value:

• A string variable contains a collection of characters string greeting = "Hello";


surrounded by double quotes: To use strings, you must include an additional header
file in the source code,
the <string> library: int z = x + y; // z will be 30 (an integer)

String Concatenation If you add two strings, the result will be a string
concatenation:
• The + operator can be used between strings to add
them together to make a new string. This is called Example:
concatenation: string x = "10";
Example: string y = "20";
string firstName = "John "; string z = x + y; // z will be 1020 (a string)
string lastName = "Doe"; If you try to add a number to a string, an error occurs.
string fullName = firstName + lastName;
String Length
cout << fullName;
To get the length of a string, use the length() function:
In the example above, we added a space after firstName
Example:
to create a space between John and Doe on output.
However, you could also add a space with quotes (" " or string txt =
' '): "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

Append cout << "The length of the txt string is: " << txt.length();

A string in C++ is actually an object, which contain Tip: You might see some C++ programs that use the
functions that can perform certain operations on strings. size() function to get the length of a string. This is just
For example, you can also concatenate strings with the an alias of length(). It is completely up to you if you
append() function: want to use length() or size()

Example: Access Strings


string firstName = "John "; You can access the characters in a string by referring to
its index number inside square brackets [].
string lastName = "Doe";
This example prints the first character in myString:
string fullName = firstName.append(lastName);
Example:
cout << fullName;
string myString = "Hello";
It is up to you whether you want to use + or append().
The major difference between the two, is that the cout << myString[0];
append() function is much faster. However, for testing
and such, it might be easier to just use +. // Outputs H

Adding Numbers and Strings This example prints the second character in myString.

NOTE! Example:

C++ uses the + operator for both addition and string myString = "Hello";
concatenation. cout << myString[1];
Numbers are added. Strings are concatenated. // Outputs e
If you add two numbers, the result will be a number:
Change String Characters
Example
To change the value of a specific character in a string,
int x = 10; refer to the index number, and use single quotes:
int y = 20; Example:
string myString = "Hello"; #include <iostream>
myString[0] = 'J'; #include <string>
cout << myString; int main()
// Outputs Jello instead of Hello {

User Input Strings std::string greeting = "Hello";

It is possible to use the extraction operator >> on cin to std::cout << greeting;
display a string entered by a user: return 0;
Example: }
string firstName; It is up to you if you want to include the standard
cout << "Type your first name: "; namespace library or not.

cin >> firstName; // get user input from the keyboard C++ Math
cout << "Your name is: " << firstName; C++ Math
// Type your first name: John
C++ has many functions that allows you to perform
// Your name is: John mathematical tasks on numbers.

However, cin considers a space (whitespace, tabs, etc.) max and min
as a terminating character, which means that it can only
display a single word (even if you type many words): The max(x,y) function can be used to find the highest
value of x and y:
From the example above, you would expect the program
to print Example:

"John Doe", but it only prints "John". cout << max(5, 10);

That's why, when working with strings, we often use the And the min(x,y) function can be used to find the lowest
getline() function to read a line of text. It takes cin as the value of x and y:
first parameter, and the string variable as second: Example:
Example: cout << min(5, 10);
string fullName; C++ <cmath> Header
cout << "Type your full name: "; Other functions, such as sqrt (square root), round
getline (cin, fullName); (rounds a number) and log (natural logarithm), can be
found in
cout << "Your name is: " << fullName;
the <cmath> header file:
// Type your full name: John Doe
Example:
// Your name is: John Doe
// Include the cmath library
Omitting Namespace
#include <cmath>
You might see some C++ programs that runs without the
cout << sqrt(64);
standard namespace library. The using namespace std
line can be omitted and replaced with the std keyword, cout << round(2.6);
followed by the :: operator for string (and cout) objects:
cout << log(2);
Example:

You might also like