×

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

round() function with example in C++

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

C++ round() function

round() function is a library function of cmath header, it is used to round the given value that is nearest to the number with halfway cases rounded away from zero, it accepts a number and returns rounded value.

Syntax

Syntax of round() function:

round(x);

Parameter(s)

x – is the number to round nearest to zero with halfway cases.

Return value

double – it returns double type value that is the rounded value of the number x.

Sample Input and Output

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

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

Example

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

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

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

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

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

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

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

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

    return 0;
}

Output

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


Comments and Discussions!

Load comments ↻





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