Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Mathematics Code Examples

There are two different functions to get the absolute value of an integer

1 2 3 4 5 6 7 8 9
/* There are two different functions to get the absolute value of an integer */ int abs(int) and of a double value double fabs(double): cout << "The absolute value of -10 is " << abs(-10) << endl; cout << "The absolute value of 12.71 is " << fabs(12.71) << endl; cout << "The absolute value of -88 is " << abs(-88) << endl; cout << "The absolute value of 4 is " << abs(4) << endl; cout << "C++ Programming Language | Happy Codings :)";
Arithmetic Operators in C++
Arithmetic Operator is used to performing mathematical operations such as addition, subtraction, multiplication, division, modulus, etc., on the given operands. For example: 6 + 3 = 9, 5 - 3 = 2, 3 * 4 = 12, etc. are the examples of arithmetic operators. Let's discuss the different types of Arithmetic Operators in the C programming.
+
Plus Operator is a simple Plus (+) Operator used to add two given operands. We can use Plus Operator with different data types such as integer, float, long, double, enumerated and string type data to add the given operand.
-
The minus operator is denoted by the minus (-) symbol. It is used to return the subtraction of the first number from the second number. The data type of the given number can be different types, such as int, float, double, long double, etc., in the programing language.
*
The multiplication operator is represented as an asterisk (*) symbol, and it is used to return the product of n1 and n2 numbers. The data type of the given number can be different types such as int, float, and double in the C programing language.
/
The division operator is an arithmetic operator that divides the first (n1) by the second (n2) number. Using division operator (/), we can divide the int, float, double and long data types variables.
%
The modulus operator is represented by the percentage sign (%), and it is used to return the remainder by dividing the first number by the second number.
++
Increment Operator is the type of Arithmetic operator, which is denoted by double plus (++) operator. It is used to increase the integer value by 1.
--
Decrement Operator is denoted by the double minus (--) symbol, which decreases the operand value by 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/* Perhaps you have warm memories of doing arithmetic drills in grade school. You can give that same pleasure to your computer. C++ uses operators to do arithmetic. It provides operators for five basic arithmetic calculations: addition, subtraction, multiplication, division, and taking the modulus. Each of these operators uses two values (called operands) to calculate a final answer. Together, the operator and its operands constitute an expression. */ #include <iostream> using namespace std; int main() { int a, b; a = 7; b = 2; // printing the sum of a and b cout << "a + b = " << (a + b) << endl; // printing the difference of a and b cout << "a - b = " << (a - b) << endl; // printing the product of a and b cout << "a * b = " << (a * b) << endl; // printing the division of a by b cout << "a / b = " << (a / b) << endl; // printing the modulo of a by b cout << "a % b = " << (a % b) << endl; return 0; }
abs() Function in C++
Absolute value. Returns the absolute value of parameter n ( /n/ ). In C++, this function is also overloaded in header <cmath> for floating-point types (see cmath abs), in header <complex> for complex numbers (see complex abs), and in header <valarray> for valarrays (see valarray abs). Basically the abs function evaluates the absolute value of the given value i.e. value after removing all the signs of negative and positive from the number. Which means it will always return a positive number.
Syntax for abs() Function in C++
#include <cstdlib> int abs (int n); long int abs (long int n); long long int abs (long long int n);
n
Integral value. Function returns the absolute value of n. abs() function - In C the input is of type 'int' whereas in C++ input is of type 'int, long int or long long int'. In C the output is of 'int' type and in C++ the output has the same data type as input. Function is defined as <cstdlib> (C Standard General Utilities Library) header file. They give the exact value of integer that is input to them as their argument.
Portability
In C, only the int version exists. For the long int equivalent see labs. For the long long int equivalent see llabs.
Data races
Concurrently calling this function is safe, causing no data races.
Exceptions
No-throw guarantee, this function throws no exceptions. If the result cannot be represented by the returned type (such as abs(INT_MIN) in an implementation with two's complement signed values), it causes undefined behavior.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* return the absolute value of parameter by abs() function code example */ // C++ code to demonstrate the example of abs() function #include <iostream> #include <cmath> using namespace std; // main() section int main() { float x; float result; x = -10; cout<<"abs("<<x<<"): "<<abs(x)<<endl; x = -1012.232; cout<<"abs("<<x<<"): "<<abs(x)<<endl; x = 1012.232; cout<<"abs("<<x<<"): "<<abs(x)<<endl; x = -.908; cout<<"abs("<<x<<"): "<<abs(x)<<endl; return 0; }
Math Library fabs() Function in C++
Compute absolute value. Returns the absolute value of x: |x|. The C or C++ library function double fabs(double x) returns the absolute value of x. Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double (defined for T being any integral type).
Syntax for Math fabs() Function in C++
#include <cmath> double fabs (double x); float fabs (float x); long double fabs (long double x); double fabs (T x); // additional overloads for integral types
x
Value whose absolute value is returned. Function returns the absolute value of x. It is defined in the cmath header file. Mathematically, fabs(num) = |num|.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* The C or C++ library function double fabs(double x) returns the absolute value of x. x - This is the floating point value. This function returns the absolute value of x. */ /* Compute absolute value by fabs() function code example */ #include <iostream> #include <cmath> using namespace std; int main () { int a, b; a = 1234; b = -344; cout << "The absolute value of " << a << " is " << fabs(a) << endl; cout << "The absolute value of " << b << " is " << fabs(b) << endl; return(0); }
Standard Output Stream (cout) in C++
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout.
Syntax for cout in C++
cout << var_name; //or cout << "Some String";
The syntax of the cout object in C++: cout << var_name; Or cout << "Some String";
<<
is the insertion operator
var_name
is usually a variable, but can also be an array element or elements of containers like vectors, lists, maps, etc. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters. The << operator can be used more than once with a combination of variables, strings, and manipulators. cout is used for displaying data on the screen. The operator << called as insertion operator or put to operator. The Insertion operator can be overloaded. Insertion operator is similar to the printf() operation in C. cout is the object of ostream class. Data flow direction is from variable to output device. Multiple outputs can be displayed using cout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* standard output stream (cout) in C++ language */ #include <iostream> using namespace std; int main() { string str = "Do not interrupt me"; char ch = 'm'; // use cout with write() cout.write(str,6); cout << endl; // use cout with put() cout.put(ch); return 0; }


Construct Binary Search tree for the unsorted data array. 'Search the element' starting from the Root of the Tree. Proceed with the search by comparing an element to the data of node
In this control structure we have only one 'if' and one 'else', however we can have multiple 'else if' blocks. This is how it looks: If none of the 'condition is true' then these statements
To copy string in C++ language, enter a string to "make copy" to another variable say str2 of same type using Function strcpy() and display the copied string on the screen as shown here