lldiv() Function with Example in C++

C++ lldiv() function: Here, we are going to learn about the lldiv() function with example of cstdlib header in C++ programming language.
Submitted by IncludeHelp, on May 28, 2020

C++ lldiv() function

lldiv() function is a library function of cstdlib header. It is used for integral division, it accepts two parameters (numerator and denominator) and returns a structure that contains the quot (quotient) and rem (remainder).

Syntax of lldiv() function:

C++11:

    lldiv_t lldiv (long long int numer, long long int denom);

Parameter(s):

  • numer – represents the value of numerator.
  • denom – represents the value of denominator.

Return value:

The return type of this function is lldiv_t, returns a structure that contains the quot (quotient) and rem (remainder).

Example:

    Input:
    long long int n = 1111222233334444555;
    long long int d = 1111222233334444;
    lldiv_t result;

    Function call:
    result = lldiv(n, d);

    Output:
    result.quot = 1000
    result.rem = 555

C++ code to demonstrate the example of lldiv() function

// C++ code to demonstrate the example of
// lldiv() function

#include <iostream>
#include <cstdlib>
using namespace std;

// main() section
int main()
{

    long long int n = 1111222233334444555;
    long long int d = 1111222233334444;
    lldiv_t result;

    result = lldiv(n, d);

    cout << "Numerator  : " << n << endl;
    cout << "Denominator: " << d << endl;
    cout << "Quotient   : " << result.quot << endl;
    cout << "Remainder  : " << result.rem << endl;
    cout << endl;

    n = 1234567890987654321;
    d = 1000000000000000000;

    result = lldiv(n, d);

    cout << "Numerator  : " << n << endl;
    cout << "Denominator: " << d << endl;
    cout << "Quotient   : " << result.quot << endl;
    cout << "Remainder  : " << result.rem << endl;
    cout << endl;

    n = 1234567890000000000;
    d = 1234567890000000000;

    result = lldiv(n, d);

    cout << "Numerator  : " << n << endl;
    cout << "Denominator: " << d << endl;
    cout << "Quotient   : " << result.quot << endl;
    cout << "Remainder  : " << result.rem << endl;
    cout << endl;

    return 0;
}

Output

Numerator  : 1111222233334444555
Denominator: 1111222233334444
Quotient   : 1000
Remainder  : 555

Numerator  : 1234567890987654321
Denominator: 1000000000000000000
Quotient   : 1
Remainder  : 234567890987654321

Numerator  : 1234567890000000000
Denominator: 1234567890000000000
Quotient   : 1
Remainder  : 0

Reference: C++ lldiv() function



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.