Perform Rounding Operations in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to perform rounding operations in C programming. You will start by declaring floating-point variables, then explore the use of various rounding functions, such as round(), floor(), and ceil(), to manipulate the values. Finally, you will print the rounded results to observe the different rounding behaviors.

This lab covers the fundamental skills of basic arithmetic operations using C, providing a solid foundation for working with floating-point numbers and their rounding techniques.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435192{{"`Perform Rounding Operations in C`"}} c/variables -.-> lab-435192{{"`Perform Rounding Operations in C`"}} c/data_types -.-> lab-435192{{"`Perform Rounding Operations in C`"}} c/math_functions -.-> lab-435192{{"`Perform Rounding Operations in C`"}} end

Declare Floating-Point Variables

In this step, you'll learn how to declare and initialize floating-point variables in C, which are essential for performing rounding operations.

First, let's create a new C file to work with floating-point variables:

cd ~/project
nano rounding.c

Now, add the following code to the file:

#include <stdio.h>

int main() {
    // Declare floating-point variables
    float num1 = 3.7;
    double num2 = 4.2;

    // Print the original values
    printf("Original float value: %.1f\n", num1);
    printf("Original double value: %.1f\n", num2);

    return 0;
}

Compile and run the program:

gcc rounding.c -o rounding
./rounding

Example output:

Original float value: 3.7
Original double value: 4.2

Let's break down the code:

  • We use float for single-precision floating-point numbers
  • We use double for double-precision floating-point numbers
  • The %.1f format specifier displays the number with one decimal place
  • num1 and num2 are initialized with decimal values

Use Functions (e.g., round, floor, ceil) to Round Values

In this step, you'll learn how to use rounding functions in C to manipulate floating-point numbers. We'll modify the previous rounding.c file to demonstrate different rounding techniques.

Open the existing file:

cd ~/project
nano rounding.c

Replace the previous content with the following code:

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

int main() {
    // Declare floating-point variables
    float num1 = 3.7;
    double num2 = 4.2;

    // Round values using different functions
    printf("Original values:\n");
    printf("num1: %.1f, num2: %.1f\n\n", num1, num2);

    // Using round() function
    printf("Round function:\n");
    printf("round(num1): %.0f\n", round(num1));
    printf("round(num2): %.0f\n\n", round(num2));

    // Using floor() function
    printf("Floor function:\n");
    printf("floor(num1): %.0f\n", floor(num1));
    printf("floor(num2): %.0f\n\n", floor(num2));

    // Using ceil() function
    printf("Ceil function:\n");
    printf("ceil(num1): %.0f\n", ceil(num1));
    printf("ceil(num2): %.0f\n", ceil(num2));

    return 0;
}

Compile the program with the math library:

gcc rounding.c -o rounding -lm
./rounding

Example output:

Original values:
num1: 3.7, num2: 4.2

Round function:
round(num1): 4
round(num2): 4

Floor function:
floor(num1): 3
floor(num2): 4

Ceil function:
ceil(num1): 4
ceil(num2): 5

Key points about rounding functions:

  • round(): Rounds to the nearest integer
  • floor(): Rounds down to the nearest integer
  • ceil(): Rounds up to the nearest integer
  • -lm flag is used to link the math library when compiling
  • %.0f format specifier displays integers without decimal places

Print Rounded Results

In this step, you'll learn how to format and print rounded results with different precision levels and formatting options.

Open the existing file:

cd ~/project
nano rounding.c

Update the code to demonstrate various printing techniques:

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

int main() {
    // Declare floating-point variables
    float num1 = 3.7;
    double num2 = 4.2;

    // Rounding with different print formats
    printf("Rounding Demonstration:\n");

    // Print rounded values with different precision
    printf("1. Rounded values:\n");
    printf("   round(num1) = %.0f\n", round(num1));
    printf("   round(num2) = %.0f\n\n", round(num2));

    // Print with field width and precision
    printf("2. Formatted rounded values:\n");
    printf("   Rounded num1 with width: %5.1f\n", round(num1));
    printf("   Rounded num2 with width: %5.1f\n\n", round(num2));

    // Print integer and floating-point representations
    printf("3. Integer and floating-point representations:\n");
    printf("   num1 as int: %d\n", (int)round(num1));
    printf("   num2 as int: %d\n", (int)round(num2));

    return 0;
}

Compile and run the program:

gcc rounding.c -o rounding -lm
./rounding

Example output:

Rounding Demonstration:
1. Rounded values:
   round(num1) = 4
   round(num2) = 4

2. Formatted rounded values:
   Rounded num1 with width:   4.0
   Rounded num2 with width:   4.0

3. Integer and floating-point representations:
   num1 as int: 4
   num2 as int: 4

Key printing techniques:

  • %.0f removes decimal places
  • %5.1f sets field width and precision
  • Type casting with (int) converts to integer
  • Different format specifiers control output appearance

Summary

In this lab, you learned how to declare floating-point variables, including float and double, and how to use various rounding functions such as round(), floor(), and ceil() to manipulate these values. You printed the original values, then used the rounding functions to display the rounded results. This allowed you to understand the different behaviors of these functions and how they can be applied to round numbers up, down, or to the nearest integer.

Other C Tutorials you may like