# Define a Macro to round a float value to nearest integer in C

C macro to round a float value to the integer: Here, we are implementing a program to convert a given float value to the nearest integer with the help of a Macro.
Submitted by IncludeHelp, on November 26, 2018

Given a float value and we have to round the value to the nearest integer with the help of Macro in C language.

Macro Definition:

```    #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
```

Example:

```    Input:
float val1=10.00f;
float val2=10.23f;
float val3=10.50f;
float val4=10.65f;

Output:
After round val1=10
After round val2=10
After round val3=11
After round val4=11
```

Program:

```#include <stdio.h>

//Macro
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))

//Main code
int main(void)
{
float val1=10.00f;
float val2=10.23f;
float val3=10.50f;
float val4=10.65f;

printf("After round val1=%d\n",round(val1));
printf("After round val2=%d\n",round(val2));
printf("After round val3=%d\n",round(val3));
printf("After round val4=%d\n",round(val4));

return 0;
}
```

Output

```After round val1=10
After round val2=10
After round val3=11
After round val4=11
```

TOP Interview Coding Problems/Challenges

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

 Recommended posts C Tips & Tricks, C++ Tips & Tricks Introduction to Linux (Its modes, Safety, Most popular Applications) Linux Best Distros of 2018 C programming optimization techniques Differences b/w C & Embedded C? Embedded C Interview Q. & A. C programming tips for Embedded Development Basic rules of writing a C program Important points (rules) to remember while writing C/C++ program Top 5 Websites for solving programming challenges Read more...

 Others... Computer G.K. (MCQ) Most viewed pages... Categories...

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.