Home » C++ programming language

lround() function with example in C++

C++ lround() function: Here, we are going to learn about the lround() function with example of cmath header in C++ programming language.
Submitted by IncludeHelp, on April 27, 2019

C++ lround() function

lround() function is a library function of cmath header, it is used to round the given value and casts to a long integer, it accepts a number and returns the integer (long int) value that is nearest to the number (with halfway cases).

Syntax of lround() function:

    lround(x);

Parameter(s): x – is the number to round nearest to zero with halfway cases.

Return value: long int – it returns long int type value that is the rounded value of the number x.

Example:

    Input:
    float x = 2.3;    
    Function call:
    lround(x);    
    Output:
    2

    Input:
    float x = 2.8;
    Function call:
    lround(x);    
    Output:
    3

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

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

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

// main() section
int main()
{
    float x;
    
    x = 15.3;
    cout<<"lround("<<x<<"): "<<lround(x)<<endl;

    x = 15.5;
    cout<<"lround("<<x<<"): "<<lround(x)<<endl;

    x = 15.8;
    cout<<"lround("<<x<<"): "<<lround(x)<<endl;

    x = -15.3;
    cout<<"lround("<<x<<"): "<<lround(x)<<endl;

    x = -15.5;
    cout<<"lround("<<x<<"): "<<lround(x)<<endl;

    x = -15.8;
    cout<<"lround("<<x<<"): "<<lround(x)<<endl;    

    return 0;
}

Output

lround(15.3): 15
lround(15.5): 16
lround(15.8): 16
lround(-15.3): -15
lround(-15.5): -16
lround(-15.8): -16



Comments and Discussions!

Load comments ↻






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