This blog is under construction

Tuesday 9 July 2013

C program to round a number

Write a C program to round a number.


  #include <stdio.h>
  #include <math.h>

  int main() {
        float num, round;

        /* Get the input from the user */
        printf("Enter the value for num:");
        scanf("%f", &num);

        /*
         * round the given number using ceil() API
         * ceil(123.45) = 124
         */
        round = ceil(num);

        /* print the rounded value */
        printf("After ceil rounding: %.2f", round);
        printf("\n");

        /*
         * round the given number using floor() API
         * floot(123.56) = 123
         */
        round = floor(num);

        /* print the rounded value */
        printf("After floor rounding: %.2f", round);
        printf("\n");

        return 0;
  }


Note:
gcc round.c -lm => link math library since we have used math functions ceil() and floor().

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the value for num:1234.123
  After ceil rounding: 1235.00
  After floor rounding: 1234.00





See Also:

No comments:

Post a Comment