Basic Arithmetic Operations in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to perform basic arithmetic operations in C programming, including declaring and initializing variables, performing addition and subtraction, multiplication and division, and combining these operations. You will write a C program that demonstrates these fundamental concepts and print the final results.

The lab covers the following steps: declaring and initializing variables, performing addition and subtraction, performing multiplication and division, combining arithmetic operations, and printing the final result. By the end of this lab, you will have a solid understanding of how to execute basic arithmetic operations in C, which is a crucial skill for any C programmer.


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/operators("`Operators`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") subgraph Lab Skills c/output -.-> lab-438262{{"`Basic Arithmetic Operations in C`"}} c/operators -.-> lab-438262{{"`Basic Arithmetic Operations in C`"}} c/math_functions -.-> lab-438262{{"`Basic Arithmetic Operations in C`"}} c/variables -.-> lab-438262{{"`Basic Arithmetic Operations in C`"}} c/data_types -.-> lab-438262{{"`Basic Arithmetic Operations in C`"}} end

Declare and Initialize Variables

In this step, you'll learn how to declare and initialize variables in C programming. Variables are essential for storing and manipulating data in your programs.

First, open the WebIDE and create a new file called arithmetic.c in the ~/project directory:

cd ~/project
touch arithmetic.c

Now, let's write a simple C program to declare and initialize variables:

#include <stdio.h>

int main() {
    // Declare and initialize integer variables
    int num1 = 10;
    int num2 = 5;

    // Declare and initialize floating-point variables
    float float1 = 3.14;
    float float2 = 2.5;

    // Print the initialized variables
    printf("Integer variables: num1 = %d, num2 = %d\n", num1, num2);
    printf("Floating-point variables: float1 = %.2f, float2 = %.2f\n", float1, float2);

    return 0;
}

Let's break down the code:

  • We declare two integer variables num1 and num2 and initialize them with values 10 and 5.
  • We also declare two floating-point variables float1 and float2 with values 3.14 and 2.5.
  • The printf() function is used to print the values of these variables.
  • %d is used for integer formatting, and %.2f is used for floating-point numbers with 2 decimal places.

Compile and run the program:

gcc arithmetic.c -o arithmetic
./arithmetic

Example output:

Integer variables: num1 = 10, num2 = 5
Floating-point variables: float1 = 3.14, float2 = 2.50

Perform Addition and Subtraction

In this step, you'll learn how to perform addition and subtraction operations with the variables you declared in the previous step. We'll modify the existing arithmetic.c file to include these arithmetic operations.

Update the program to include addition and subtraction:

#include <stdio.h>

int main() {
    // Declare and initialize integer variables
    int num1 = 10;
    int num2 = 5;

    // Declare and initialize floating-point variables
    float float1 = 3.14;
    float float2 = 2.5;

    // Perform addition with integers
    int sum_int = num1 + num2;

    // Perform subtraction with integers
    int diff_int = num1 - num2;

    // Perform addition with floating-point numbers
    float sum_float = float1 + float2;

    // Perform subtraction with floating-point numbers
    float diff_float = float1 - float2;

    // Print the results of addition and subtraction
    printf("Integer Addition: %d + %d = %d\n", num1, num2, sum_int);
    printf("Integer Subtraction: %d - %d = %d\n", num1, num2, diff_int);
    printf("Floating-point Addition: %.2f + %.2f = %.2f\n", float1, float2, sum_float);
    printf("Floating-point Subtraction: %.2f - %.2f = %.2f\n", float1, float2, diff_float);

    return 0;
}

Let's break down the new code:

  • We've added integer addition and subtraction using num1 and num2
  • We've also added floating-point addition and subtraction using float1 and float2
  • The results are stored in new variables: sum_int, diff_int, sum_float, and diff_float
  • We use printf() to display the results of these operations

Compile and run the updated program:

gcc arithmetic.c -o arithmetic
./arithmetic

Example output:

Integer Addition: 10 + 5 = 15
Integer Subtraction: 10 - 5 = 5
Floating-point Addition: 3.14 + 2.50 = 5.64
Floating-point Subtraction: 3.14 - 2.50 = 0.64

Perform Multiplication and Division

In this step, you'll learn how to perform multiplication and division operations with the variables you've been working with. We'll continue modifying the arithmetic.c file to include these arithmetic operations.

Update the program to include multiplication and division:

#include <stdio.h>

int main() {
    // Declare and initialize integer variables
    int num1 = 10;
    int num2 = 5;

    // Declare and initialize floating-point variables
    float float1 = 3.14;
    float float2 = 2.5;

    // Previous addition and subtraction operations
    int sum_int = num1 + num2;
    int diff_int = num1 - num2;
    float sum_float = float1 + float2;
    float diff_float = float1 - float2;

    // Perform multiplication with integers
    int product_int = num1 * num2;

    // Perform division with integers
    int quotient_int = num1 / num2;

    // Perform multiplication with floating-point numbers
    float product_float = float1 * float2;

    // Perform division with floating-point numbers
    float quotient_float = float1 / float2;

    // Print the results of multiplication and division
    printf("Integer Multiplication: %d * %d = %d\n", num1, num2, product_int);
    printf("Integer Division: %d / %d = %d\n", num1, num2, quotient_int);
    printf("Floating-point Multiplication: %.2f * %.2f = %.2f\n", float1, float2, product_float);
    printf("Floating-point Division: %.2f / %.2f = %.2f\n", float1, float2, quotient_float);

    return 0;
}

Let's break down the new code:

  • We've added integer multiplication using num1 * num2
  • We've added integer division using num1 / num2
  • We've also added floating-point multiplication and division
  • The results are stored in new variables: product_int, quotient_int, product_float, and quotient_float
  • We use printf() to display the results of these operations

Note: Integer division in C truncates the decimal part, so 10 / 5 will result in 2.

Compile and run the updated program:

gcc arithmetic.c -o arithmetic
./arithmetic

Example output:

Integer Multiplication: 10 * 5 = 50
Integer Division: 10 / 5 = 2
Floating-point Multiplication: 3.14 * 2.50 = 7.85
Floating-point Division: 3.14 / 2.50 = 1.26

Combine Arithmetic Operations

In this step, you'll learn how to combine multiple arithmetic operations in a single expression. This will help you understand how C handles complex calculations and the order of operations.

Update the program to combine arithmetic operations:

#include <stdio.h>

int main() {
    // Declare and initialize variables
    int num1 = 10;
    int num2 = 5;
    int num3 = 3;

    float float1 = 3.14;
    float float2 = 2.5;

    // Combine arithmetic operations with integers
    int combined_int = num1 + num2 * num3;
    int complex_int = (num1 + num2) * num3;

    // Combine arithmetic operations with floating-point numbers
    float combined_float = float1 + float2 * num3;
    float complex_float = (float1 + float2) / num2;

    // Print the results of combined operations
    printf("Integer Combined Operation 1: %d + %d * %d = %d\n", num1, num2, num3, combined_int);
    printf("Integer Combined Operation 2: (%d + %d) * %d = %d\n", num1, num2, num3, complex_int);
    printf("Floating-point Combined Operation 1: %.2f + %.2f * %d = %.2f\n", float1, float2, num3, combined_float);
    printf("Floating-point Combined Operation 2: (%.2f + %.2f) / %d = %.2f\n", float1, float2, num2, complex_float);

    return 0;
}

Let's break down the new code:

  • We've added a new integer variable num3
  • We demonstrate two types of combined operations:
    1. Standard order of operations (multiplication before addition)
    2. Using parentheses to change the order of operations
  • The results show how parentheses can significantly change the calculation result
  • We use printf() to display the results of these combined operations

Compile and run the updated program:

gcc arithmetic.c -o arithmetic
./arithmetic

Example output:

Integer Combined Operation 1: 10 + 5 * 3 = 25
Integer Combined Operation 2: (10 + 5) * 3 = 45
Floating-point Combined Operation 1: 3.14 + 2.50 * 3 = 10.64
Floating-point Combined Operation 2: (3.14 + 2.50) / 5 = 1.13

Key observations:

  • In the first operation, 5 * 3 is calculated first, then added to 10
  • In the second operation, (10 + 5) is calculated first, then multiplied by 3
  • The same principles apply to floating-point numbers

Print the Final Result

In this final step, you'll learn how to create a comprehensive arithmetic calculation program that performs multiple operations and prints the final results in a formatted manner.

Create the final version of the program:

#include <stdio.h>

int main() {
    // Declare and initialize variables
    int num1 = 10;
    int num2 = 5;
    int num3 = 3;

    float float1 = 3.14;
    float float2 = 2.5;

    // Perform arithmetic operations
    int sum = num1 + num2;
    int difference = num1 - num2;
    int product = num1 * num2;
    int quotient = num1 / num2;

    float float_sum = float1 + float2;
    float float_difference = float1 - float2;
    float float_product = float1 * float2;
    float float_quotient = float1 / float2;

    // Combined complex operation
    float final_result = (num1 + num2) * float1 / num3;

    // Print a formatted final result summary
    printf("Arithmetic Operations Summary\n");
    printf("==============================\n");
    printf("Integer Operations:\n");
    printf("  Addition:      %d + %d = %d\n", num1, num2, sum);
    printf("  Subtraction:   %d - %d = %d\n", num1, num2, difference);
    printf("  Multiplication:%d * %d = %d\n", num1, num2, product);
    printf("  Division:      %d / %d = %d\n\n", num1, num2, quotient);

    printf("Floating-Point Operations:\n");
    printf("  Addition:      %.2f + %.2f = %.2f\n", float1, float2, float_sum);
    printf("  Subtraction:   %.2f - %.2f = %.2f\n", float1, float2, float_difference);
    printf("  Multiplication:%.2f * %.2f = %.2f\n", float1, float2, float_product);
    printf("  Division:      %.2f / %.2f = %.2f\n\n", float1, float2, float_quotient);

    printf("Final Complex Calculation:\n");
    printf("  (%d + %d) * %.2f / %d = %.2f\n", num1, num2, float1, num3, final_result);

    return 0;
}

Let's break down the new code:

  • We've created separate variables for each arithmetic operation
  • We've added a complex final calculation combining multiple operations
  • We use formatted printf() statements to create a clean, readable output
  • Different formatting is used for integers (%d) and floating-point numbers (%.2f)

Compile and run the program:

gcc arithmetic.c -o arithmetic
./arithmetic

Example output:

Arithmetic Operations Summary
==============================
Integer Operations:
  Addition:      10 + 5 = 15
  Subtraction:   10 - 5 = 5
  Multiplication:10 * 5 = 50
  Division:      10 / 5 = 2

Floating-Point Operations:
  Addition:      3.14 + 2.50 = 5.64
  Subtraction:   3.14 - 2.50 = 0.64
  Multiplication:3.14 * 2.50 = 7.85
  Division:      3.14 / 2.50 = 1.26

Final Complex Calculation:
  (10 + 5) * 3.14 / 3 = 15.70

Summary

In this lab, you learned how to declare and initialize variables, including both integer and floating-point types. You then performed basic arithmetic operations such as addition, subtraction, multiplication, and division using these variables. Finally, you combined multiple arithmetic operations and printed the final result. The key learning points covered in this lab include variable declaration and initialization, as well as the syntax and usage of common arithmetic operators in C programming.

Other C Tutorials you may like