Use the Modulus Operator in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to use the modulus operator in C programming to compute the remainder of a division operation. The lab covers the following steps:

Declare integer variables and input values from the user, then compute the remainder using the modulus operator and print the result. This hands-on exercise will help you understand the basic arithmetic operations in C and how to apply the modulus operator effectively.


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/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435358{{"`Use the Modulus Operator in C`"}} c/variables -.-> lab-435358{{"`Use the Modulus Operator in C`"}} c/operators -.-> lab-435358{{"`Use the Modulus Operator in C`"}} c/user_input -.-> lab-435358{{"`Use the Modulus Operator in C`"}} c/math_functions -.-> lab-435358{{"`Use the Modulus Operator in C`"}} end

Declare Integer Variables and Input Values

In this step, you'll learn how to declare integer variables and input values in a C program for performing modulus operations. We'll create a simple program that demonstrates basic variable declaration and user input.

First, let's create a new C file in the project directory:

cd ~/project
nano modulus_example.c

Now, enter the following C code:

#include <stdio.h>

int main() {
    // Declare integer variables
    int dividend, divisor;

    // Prompt user for input
    printf("Enter the dividend (number to be divided): ");
    scanf("%d", &dividend);

    printf("Enter the divisor (number to divide by): ");
    scanf("%d", &divisor);

    // Print the input values
    printf("Dividend: %d\n", dividend);
    printf("Divisor: %d\n", divisor);

    return 0;
}

Let's break down the code:

  • int dividend, divisor; declares two integer variables to store the numbers
  • printf() is used to prompt the user for input
  • scanf() reads integer input from the user
  • %d is the format specifier for integers
  • &dividend and &divisor pass the memory addresses to store the input values

Compile the program:

gcc modulus_example.c -o modulus_example

Example output when running the program:

Enter the dividend (number to be divided): 17
Enter the divisor (number to divide by): 5
Dividend: 17
Divisor: 5

Compute the Remainder Using Modulus

In this step, you'll learn how to use the modulus operator (%) to compute the remainder of a division operation in C. We'll modify the previous program to calculate and display the remainder.

Open the existing file and update the code:

cd ~/project
nano modulus_example.c

Replace the previous code with the following:

#include <stdio.h>

int main() {
    // Declare integer variables
    int dividend, divisor, remainder;

    // Prompt user for input
    printf("Enter the dividend (number to be divided): ");
    scanf("%d", &dividend);

    printf("Enter the divisor (number to divide by): ");
    scanf("%d", &divisor);

    // Calculate remainder using modulus operator
    remainder = dividend % divisor;

    // Print the input values and remainder
    printf("Dividend: %d\n", dividend);
    printf("Divisor: %d\n", divisor);
    printf("Remainder: %d\n", remainder);

    return 0;
}

Key changes in this version:

  • Added a new remainder variable to store the result
  • Used the modulus operator % to calculate the remainder
  • Added a print statement to display the remainder

Compile the updated program:

gcc modulus_example.c -o modulus_example

Example output when running the program:

Enter the dividend (number to be divided): 17
Enter the divisor (number to divide by): 5
Dividend: 17
Divisor: 5
Remainder: 2

In this example, 17 divided by 5 is 3 with a remainder of 2. The modulus operator % returns exactly this remainder.

Print the Result

In this step, you'll learn how to format and print the results of modulus operations in a more meaningful way. We'll enhance the previous program to provide a clear explanation of the calculation.

Open the existing file and update the code:

cd ~/project
nano modulus_example.c

Replace the previous code with the following:

#include <stdio.h>

int main() {
    // Declare integer variables
    int dividend, divisor, remainder, quotient;

    // Prompt user for input
    printf("Enter the dividend (number to be divided): ");
    scanf("%d", &dividend);

    printf("Enter the divisor (number to divide by): ");
    scanf("%d", &divisor);

    // Calculate quotient and remainder
    quotient = dividend / divisor;
    remainder = dividend % divisor;

    // Print detailed explanation of the division
    printf("\nDivision Calculation:\n");
    printf("%d ÷ %d = %d remainder %d\n",
           dividend, divisor, quotient, remainder);

    // Verification equation
    printf("\nVerification:\n");
    printf("%d = %d × %d + %d\n",
           dividend, divisor, quotient, remainder);

    return 0;
}

Compile the updated program:

gcc modulus_example.c -o modulus_example

Example output when running the program:

Enter the dividend (number to be divided): 17
Enter the divisor (number to divide by): 5

Division Calculation:
17 ÷ 5 = 3 remainder 2

Verification:
17 = 5 × 3 + 2

Key changes in this version:

  • Added quotient calculation using integer division
  • Included more detailed output showing the division process
  • Added a verification line to demonstrate the mathematical relationship

Summary

In this lab, you learned how to declare integer variables and input values in a C program for performing modulus operations. You created a simple program that prompts the user for a dividend and divisor, and then you used the modulus operator to compute the remainder of the division operation. The program then displayed the input values and the calculated remainder.

Additionally, you learned how to compile the C program and run it, observing the program's output. This lab provided a hands-on introduction to the modulus operator and its practical application in C programming.

Other C Tutorials you may like