round() : Nearest integer value of a number

#include <stdio.h>
int main() {
printf("\nNumber = %.2f", round(12.38)); // Number = 12.00
printf("\nNumber = %.2f", round(12.89)); // Number = 13.00
printf("\nNumber = %.2f", round(-12.32)); // Number = -12.00
printf("\nNumber = %.2f", round(-12.95)); // Number = -13.00
return 0;
}
We will get nearest integer part of a decimal value by using round() function. If the decimal part is less than .5 then lower side integer is returned. If decimal part is more than or equal .5 then higher side is returned.
round(x) 
x = input number

Example with integer variables

#include 
#include 
int main(void){
     int  x1,x2,x3,x4;
     x1=round(13.345);
     x2=round(12.656);
     x3=round(-13.21);
     x4=round(-14.67);
     printf("x1=%d , x2=%d,x3=%d,x4=%d", x1,x2,x3,x4);
     return 0;
}
The above code will give this output.
x1=13, x2=13, x3=-13,x4=-15

Example with float variables

#include <stdio.h>
#include <math.h>
int main(void){
     float  x1,x2,x3,x4;
     x1=round(13.545);
     x2=round(12.656);
     x3=round(-13.51);
     x4=round(-14.67);
     printf("x1=%f , x2=%f,x3=%f,x4=%f", x1,x2,x3,x4);
     return 0;
}
Output is here
x1=14.000000, x2=13.000000, x3=-14.000000,x4=-15.000000

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer