×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

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

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.

Sample Input and Output

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

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

Example

// 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.