Perform Integer Arithmetic in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to perform basic arithmetic operations using integer variables in C programming. The lab covers the following steps:

Declare Integer Variables and Assign Values: You will learn how to declare integer variables and assign values to them. This is the foundation for performing arithmetic operations.

Execute Arithmetic Operations Between Integers: You will explore how to perform addition, subtraction, multiplication, and division between integer variables. This will demonstrate the practical application of integer arithmetic in C.


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/BasicsGroup -.-> c/operators("`Operators`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435356{{"`Perform Integer Arithmetic in C`"}} c/variables -.-> lab-435356{{"`Perform Integer Arithmetic in C`"}} c/data_types -.-> lab-435356{{"`Perform Integer Arithmetic in C`"}} c/operators -.-> lab-435356{{"`Perform Integer Arithmetic in C`"}} c/math_functions -.-> lab-435356{{"`Perform Integer Arithmetic in C`"}} end

Declare Integer Variables and Assign Values

In this step, you will learn how to declare integer variables and assign values in C programming. Integer variables are fundamental for storing whole numbers and performing arithmetic operations.

First, let's create a new C file to demonstrate integer variable declaration:

cd ~/project
nano integer_variables.c

Now, add the following code to the file:

#include <stdio.h>

int main() {
    // Declare integer variables
    int num1 = 10;
    int num2 = 20;
    int num3;

    // Assign a value to num3
    num3 = 30;

    // Print the values of the variables
    printf("num1: %d\n", num1);
    printf("num2: %d\n", num2);
    printf("num3: %d\n", num3);

    return 0;
}

Let's break down the code:

  • int num1 = 10; declares an integer variable num1 and initializes it with the value 10
  • int num2 = 20; declares another integer variable num2 with the value 20
  • int num3; declares an integer variable num3 without an initial value
  • num3 = 30; assigns the value 30 to num3
  • printf() statements are used to print the values of the variables

Compile and run the program:

gcc integer_variables.c -o integer_variables
./integer_variables

Example output:

num1: 10
num2: 20
num3: 30

Execute Arithmetic Operations Between Integers

In this step, you will learn how to perform basic arithmetic operations between integers in C programming. We'll build upon the previous step and demonstrate addition, subtraction, multiplication, and division.

Let's modify the previous file to include arithmetic operations:

cd ~/project
nano integer_operations.c

Add the following code to the file:

#include <stdio.h>

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

    // Addition
    int sum = num1 + num2;
    printf("Addition: %d + %d = %d\n", num1, num2, sum);

    // Subtraction
    int difference = num1 - num2;
    printf("Subtraction: %d - %d = %d\n", num1, num2, difference);

    // Multiplication
    int product = num1 * num2;
    printf("Multiplication: %d * %d = %d\n", num1, num2, product);

    // Division
    int quotient = num1 / num2;
    int remainder = num1 % num2;
    printf("Division: %d / %d = %d (Remainder: %d)\n", num1, num2, quotient, remainder);

    return 0;
}

Let's break down the arithmetic operations:

  • + performs addition
  • - performs subtraction
  • * performs multiplication
  • / performs integer division
  • % calculates the remainder of integer division

Compile and run the program:

gcc integer_operations.c -o integer_operations
./integer_operations

Example output:

Addition: 20 + 10 = 30
Subtraction: 20 - 10 = 10
Multiplication: 20 * 10 = 200
Division: 20 / 10 = 2 (Remainder: 0)

Print Integer Results

In this step, you will learn different ways to print integer results in C programming using various formatting options with the printf() function.

Let's create a new file to demonstrate different printing techniques:

cd ~/project
nano print_integers.c

Add the following code to the file:

#include <stdio.h>

int main() {
    // Declare integer variables
    int num1 = 42;
    int num2 = 100;
    int sum = num1 + num2;

    // Basic integer printing
    printf("Basic printing:\n");
    printf("num1 = %d\n", num1);
    printf("num2 = %d\n", num2);
    printf("Sum = %d\n\n", sum);

    // Formatted printing with width and alignment
    printf("Formatted printing:\n");
    printf("Right-aligned (width 5): %5d\n", num1);
    printf("Left-aligned (width 5): %-5d\n", num2);

    // Printing with leading zeros
    printf("Printing with leading zeros:\n");
    printf("Number with 4 digits: %04d\n", num1);
    printf("Number with 6 digits: %06d\n", sum);

    return 0;
}

Compile and run the program:

gcc print_integers.c -o print_integers
./print_integers

Example output:

Basic printing:
num1 = 42
num2 = 100
Sum = 142

Formatted printing:
Right-aligned (width 5):    42
Left-aligned (width 5): 100
Printing with leading zeros:
Number with 4 digits: 0042
Number with 6 digits: 000142

Let's break down the printing techniques:

  • %d is the format specifier for integers
  • %5d right-aligns the number in a 5-character width
  • %-5d left-aligns the number in a 5-character width
  • %04d prints the number with leading zeros to fill 4 digits
  • %06d prints the number with leading zeros to fill 6 digits

Summary

In this lab, you learned how to declare integer variables and assign values, as well as perform basic arithmetic operations between integers in C programming. You started by declaring integer variables and initializing them with different values. Then, you executed arithmetic operations such as addition, subtraction, multiplication, and division, and printed the results. These fundamental skills are essential for working with whole numbers in C programs.

Other C Tutorials you may like