
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
lldiv() function in C++ STL
The lldiv() function is used to perform division operation on long long integers and return both quotient and remainder. In this article, we will learn how to use the lldiv() function from the Standard Template Library (STL) in C++.
What is lldiv()?
The lldiv() is a in-built function in C++ that is used to divide one long long integer by another and return the result as a structure that contains both the quotient and remainder. This function is useful when both values are needed after a division operation. The return type of this function is a structure called lldiv_t, which contains two members: quot (quotient) and rem (remainder).
For example, the code below shows how the lldiv() function returns both quotient and remainder after divison:
// Call the function lldiv_t result = lldiv( 10000000000, 3000); cout << result.quot << result.rem; // Outputs: 3333333 1000
Using lldiv() Function in STL
The lldiv() function is defined in the <cstdlib> header of STL. It performs division on two long long integers. Below are some points about this function:
- Header: <cstdlib>
-
Syntax:
lldiv_t lldiv(long long numer, long long denom);
-
Parameters:
- numer - the numerator (dividend)
- denom - the denominator (divisor)
- Return: Returns a structure of type lldiv_t containing quotient and remainder.
Steps to Use lldiv() in C++ STL
Following are steps/algorithm to use lldiv() using C++ STL:
- Include the <cstdlib> header file.
- Declare two long long integers as numerator and denominator.
- Call the lldiv() function with the two numbers.
- Use the returned structure to access quotient and remainder.
C++ Program to Implement lldiv() using STL
The below code is the implementation of the above algorithm in C++ language.
#include <iostream> #include <cstdlib> using namespace std; int main() { long long numerator = 10000000000; long long denominator = 3000; lldiv_t result = lldiv(numerator, denominator); cout << "Quotient: " << result.quot << endl; cout << "Remainder: " << result.rem << endl; return 0; }
The output of above code will be:
Quotient: 3333333 Remainder: 1000
Time and Space Complexity
Time Complexity: O(1), as it is a basic arithmetic operation.
Space Complexity: O(1), as it uses constant space to store the result.