Q:

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

belongs to collection: C Preprocessors Programs

0

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

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to print the function names defined in t... >>
<< Define a Macro to find total number of elements in...