 Open in App
Not now

# Top 10 Most Used Inbuilt C++ functions for Competitive Programming

• Difficulty Level : Easy
• Last Updated : 17 Jun, 2022

In this article, we will discuss about the 10 most used inbuilt functions of C++ which will help you to save time and make code concise as well during competitive programming.

List of top 10 inbuilt functions in C++

### 1. pow( )

This function helps to find the value of a number raised to another number. It always takes to values of double data type as parameters (Also accepts int data type) and the result is of double data type.

pow() function is defined inside the cmath header file.
#include <cmath>

Syntax:

pow(base, exponent)
The result of this function will be baseexponent

Time Complexity:  O(exponent).

Below are some examples to illustrate the working of pow() method in C++:

## C++

 `// CPP program to illustrate` `// power function` `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``double` `x = 6.1, y = 4.8;`   `    ``// Storing the answer in result.` `    ``double` `result = ``pow``(x, y);`   `    ``// Printing the result upto 2` `    ``// decimal place` `    ``cout << fixed << setprecision(2) << result << endl;`   `    ``return` `0;` `}`

Output

`5882.79`

### 2.sqrt()

This function helps to find the square root of any number. It takes floating pointer or integer data type as an argument. The result is returned after rounding it according to the required data type.

sqrt  function is defined inside cmath header file.
#include <cmath>

Syntax:

sqrt(N);

Time Complexity: θ(log(N))

Below are some examples to illustrate the working of sqrt() method in C++:

## C++

 `// CPP Program to demonstrate errors in double sqrt()` `#include ` `#include ` `using` `namespace` `std;`   `// Driver Code` `int` `main()` `{` `    ``int` `x = 24;` `    ``double` `answer;`   `    ``answer = ``sqrt``(x);`   `    ``// Printing square root of 24.` `    ``cout << answer << endl;` `    ``return` `0;` `}`

Output

`4.89898`

### 3.min():

This function helps to find the minimum between two numbers. It takes two numbers of the same data type as arguments and returns the value of the minimum.

This function is defined in algorithm header file.
#include <algorithm>

Syntax:

min(value1, value2);

Time Complexity: O(1)

Below are some examples to illustrate the working of min() method in C++:

## C++

 `// C++ program to demonstrate the use of std::min` `#include ` `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``int` `a = 5;` `    ``int` `b = 7;`   `    ``cout << std::min(a, b) << ``"\n"``;` `    ``return` `0;` `}`

Output

`5`

### 4. max()

It helps in finding the maximum between two values. This function takes two values of the same data type as arguments and returns the value of the maximum element.

This function is defined in algorithm header file.
#include <algorithm>

Syntax:

max(value1, value2);

Time Complexity: O(1).

Below are some examples to illustrate the working of max() method in C++:

## C++

 `// C++ program to demonstrate use of max()` `#include ` `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``int` `a = 112, b = 123;`   `    ``// Comparing a and b` `    ``cout << std::max(a, b) << ``"\n"``;`   `    ``// Returns the first one if both the numbers` `    ``// are same` `    ``cout << std::max(7, 7);` `    ``return` `0;` `}`

Output

```123
7```

### 5. swap()

This function is used for swapping two numbers. It takes two values of the same data type as arguments and swaps their value.

#include <algorithm> (until C++11)
#include <utility> (since C++11)
#include <string_view> (since C++17)

Syntax:

swap(value1, value2);

Time Complexity: O(1)

Below are some examples to illustrate the working of swap() method in C++:

## C++14

 `// C++ program for illustration` `// of swap() function` `#include ` `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``int` `a = 10;` `    ``int` `b = 20;` `    ``cout << ``"Value of a before: "` `<< a << endl;` `    ``cout << ``"Value of b before: "` `<< b << endl;`   `    ``// swap values of the variables` `    ``swap(a, b);` `    ``cout << ``"Value of a now: "` `<< a << endl;` `    ``cout << ``"Value of b now: "` `<< b << endl;` `    ``return` `0;` `}`

Output

```Value of a before: 10
Value of b before: 20
Value of a now: 20
Value of b now: 10```

### 6. gcd()

This function is used to find the GCD of two numbers. It takes two values of the same data type as arguments and returns the GCD of them.

This function is defined in algorithm header file for C++14
#include <algorithm>
#include <numeric> (for C++17)

Syntax:

__gcd(value1, value2);  [for C++14]
gcd(value1, value2);   [for C++17]

Time Complexity: O(log(max(value1, value2)))).

Below are some examples to illustrate the working of gcd() method in C++:

## C++

 `// CPP program to illustrate` `// gcd function of C++ STL` `#include ` `#include ` `// #include for C++17`   `using` `namespace` `std;`   `int` `main()` `{` `    ``int` `a = 6, b = 20;` `    ``int` `ans = __gcd(a, b);` `    ``// int ans = gcd(a, b) for C++17`   `    ``cout << ``"gcd(6, 20) = "` `<< ans << endl;` `    ``return` `0;` `}`

Output

`gcd(6, 20) = 2`

### 7. toupper()

This function is used for converting a lowercase character to uppercase.

This function is defined in cctype header file
#include <cctype>

Syntax:

toupper(‘ch’);   where ch is lower case character.

Time Complexity: O(1).

Below are some examples to illustrate the working of toupper() method in C++:

## C++

 `// C++ program to illustrate toupper() method` `#include ` `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``int` `j = 0;` `    ``char` `str[] = ``"geekforgeeks"``;` `    ``char` `ch;`   `    ``while` `(str[j]) {` `        ``ch = str[j];` `        ``putchar``(``toupper``(ch));` `        ``j++;` `    ``}`   `    ``return` `0;` `}`

Output

`GEEKFORGEEKS`

### 8. tolower():

This function is used for converting an uppercase character to lowercase.

This function is defined in cctype header file.
#include <cctype>

Syntax:

tolower(ch);   where ch is an uppercase character.

Time Complexity: O(1).

Below are some examples to illustrate the working of tolower() method in C++:

## C++

 `// C++ program to illustrate tolower() method` `#include ` `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``int` `j = 0;` `    ``char` `str[] = ``"GEEKSFORGEEKS"``;` `    ``char` `ch;`   `    ``while` `(str[j]) {` `        ``ch = str[j];` `        ``putchar``(``tolower``(ch));` `        ``j++;` `    ``}`   `    ``return` `0;` `}`

Output

`geeksforgeeks`

### 9. floor():

This function returns the largest possible integer value which is less than or equal to a given argument. It takes a floating number as an argument and returns an integer value.

floor function is defined in cmath header file
#include <cmath>

Syntax:

floor(value);

Time Complexity: O(1)

Below are some examples to illustrate the working of floor() method in C++:

## C++

 `// C++ program to demonstrate floor function` `#include ` `#include ` `using` `namespace` `std;`   `// Driver function` `int` `main()` `{` `    ``// Using floor function which returns` `    ``// floor of input value` `    ``cout << ``"Floor is: "` `<< ``floor``(2.3) << ``"\n"``;` `    ``cout << ``"Floor is: "` `<< ``floor``(-2.3) << ``"\n"``;`   `    ``return` `0;` `}`

Output

```Floor is: 2
Floor is: -3```

### 10. Ceil():

This function is just the opposite of floor(), It returns the smallest possible integer value which is greater than or equal to the given argument. It takes a floating value as an argument and returns an integer value.

ceil function is defined in cmath header file
#include <cmath>

Syntax:

ceil(value);

Time Complexity: O(1)

Below are some examples to illustrate the working of ceil() method in C++:

## C++

 `// C++ program to demonstrate ceil function` `#include ` `#include ` `using` `namespace` `std;`   `// Driver function` `int` `main()` `{` `    ``// Using ceil function which return` `    ``// floor of input value` `    ``cout << ``" Ceil is: "` `<< ``ceil``(2.3) << ``"\n"``;` `    ``cout << ``" Ceil is: "` `<< ``ceil``(-2.3) << ``"\n"``;`   `    ``return` `0;` `}`

Output

``` Ceil is: 3
Ceil is: -2```

My Personal Notes arrow_drop_up
Related Articles