Operators Assignment 5 — Find the Remainder Using the '%' Operator
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program: Find the Remainder Using the '%' Operator

📑 On this page:
  • The modulus operator
  • Complete C Program
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions
📚 In this program, you will learn:
  • How the % operator computes the remainder of integer division
  • Why % works only on integers (not on double or float)
  • How the remainder relates to the quotient: a = b * (a / b) + (a % b)
  • How negative operands affect the sign of the remainder
  • Why % gives a remainder of 0 for exact divisibility

The modulus operator

The modulus operator % (often called "mod") returns the remainder after integer division. For a % b, the result is what's left over when you divide a by b. If a = 10 and b = 3, then 10 / 3 = 3 (quotient) with a remainder of 1, so 10 % 3 = 1.

The relationship between division and modulus is captured by the division algorithm: for any integers a and b (with b ≠ 0),

a = b * (a / b) + (a % b)

In words: the dividend equals the divisor times the quotient, plus the remainder. This is the fundamental identity that connects / and % — they're two views of the same division operation. When the remainder is zero, a is an exact multiple of b.

Critical constraint: % works only on integers. Applying % to a double or float is a compile error in C. For floating-point remainder, use fmod() from <math.h> — for example, fmod(7.5, 2.0) returns 1.5.

Sign rules for negative operands: until C99, the sign of a % b was implementation-defined. Since C99, the rule is that a % b has the same sign as a (the dividend). So −7 % 2 = −1, 7 % −2 = 1, and −7 % −2 = −1. In all cases, the remainder's magnitude is less than the divisor's magnitude.

Remainder of two numbers in C — reading two integers with scanf, computing the remainder with the % operator, and printing it with printf.

Complete C Program

Here is the program — find the remainder of two integers:

Program: Remainder of two integers

#include <stdio.h>

int main(void) {
    int a, b, rem;

    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);

    if (b == 0) {
        printf("Error: modulus by zero is undefined\n");
        return 1;
    }

    rem = a % b;

    printf("Remainder = %d\n", rem);

    return 0;
}
                                

A variant that also prints the quotient — useful to verify the division algorithm:

Variant: Print quotient and remainder together

#include <stdio.h>

int main(void) {
    int a, b;

    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);

    if (b == 0) {
        printf("Error: division by zero\n");
        return 1;
    }

    printf("%d / %d = %d (quotient)\n", a, b, a / b);
    printf("%d %% %d = %d (remainder)\n", a, b, a % b);
    printf("Check: %d = %d * %d + %d\n", a, b, a / b, a % b);

    return 0;
}
                                

Note the %% in the second printf — that's the escape sequence for printing a literal % character. Without the doubled percent, printf would misinterpret % as the start of a format specifier.

Program Output

Sample run:

Enter two integers: 10 3
Remainder = 1
                            

10 ÷ 3 = 3 remainder 1, so 10 % 3 = 1. Correct.

Exact divisibility:

Enter two integers: 15 5
Remainder = 0
                            

15 ÷ 5 = 3 remainder 0. When the remainder is zero, the first number is exactly divisible by the second — useful for checking even/odd (n % 2 == 0) or multiples.

Negative dividend:

Enter two integers: -7 2
Remainder = -1
                            

−7 ÷ 2 = −3 remainder −1. The remainder has the sign of the dividend (−7), not the divisor. This is the C99 rule.

The variant output:

Enter two integers: 17 5
17 / 5 = 3 (quotient)
17 % 5 = 2 (remainder)
Check: 17 = 5 * 3 + 2
                            

The check line verifies the division algorithm: 17 = 5 × 3 + 2. Correct.

Modulus by zero:

Enter two integers: 10 0
Error: modulus by zero is undefined
                            

The check prevents a crash. In C, a % 0 is undefined behavior — just like division by zero.

Step-by-Step Explanation

1. Declarations: int a, b, rem; — the modulus operator requires integer operands, so a and b must be int (or another integer type). Using double here would be a compile error.

2. Input: scanf("%d %d", &a, &b) reads two integers. The & is essential.

3. Zero check: if (b == 0) catches modulus by zero. Like division, a % 0 is undefined behavior — you must check before computing.

4. The modulus: rem = a % b computes the remainder. For 10 % 3, the CPU performs integer division and keeps the remainder (1). For 15 % 5, the remainder is 0 (exact division). For −7 % 2, the remainder is −1 (sign matches the dividend −7).

5. Output: printf("Remainder = %d\n", rem) prints the result. For the variant that prints the modulus symbol itself, use %% (escaped percent) in the format string.

6. The division algorithm check: in the variant, a = b * (a / b) + (a % b) is printed explicitly. For 17, 5, this gives 17 = 5 × 3 + 2, confirming that quotient × divisor + remainder = dividend. This is the fundamental relationship between / and %.

🎯 Key idea: % returns the remainder after integer division. It works only on integers (for double, use fmod). The remainder has the same sign as the dividend. Common uses: checking even/odd, extracting digits, cyclic behaviors, hash functions.

Frequently Asked Questions

1. Why can't I use % with double or float?

Because the modulus operator is defined only for integer types in C. Floating-point "remainder" is a different concept (it's the leftover after floating-point division, which involves precision), and it's provided by fmod() in <math.h>. So 10.5 % 3 is a compile error, but fmod(10.5, 3) returns 1.5.

2. Why is −7 % 2 equal to −1, not 1?

Because since C99, the remainder has the same sign as the dividend (the first operand). −7 is negative, so the remainder is −1. In older C dialects, the sign was implementation-defined — the same expression might have returned 1 on some systems. If you need a non-negative remainder (like for cyclic indexing), use ((a % b) + b) % b to normalize it.

3. What happens if I do % by zero?

Undefined behavior — just like division by zero. On most systems, it triggers a SIGFPE signal (crash). Always check the divisor before computing: if (b != 0) rem = a % b;

4. How can I use % to check if a number is even or odd?

Use n % 2. If it's 0, the number is even (divisible by 2). If it's 1 (or −1 for negative numbers), the number is odd. So if (n % 2 == 0) printf("even"); else printf("odd");. This works for any integer, including negatives — but be aware that −5 % 2 = −1, not 1. Comparing against 0 is safer than comparing against 1.

5. What's the difference between % and fmod()?

% works on integers and returns an integer remainder. fmod() works on floating-point and returns a floating-point remainder. For integers, 7 % 3 = 1. For doubles, fmod(7.5, 3.0) = 1.5. If you're working with integers, use %; if with floating-point, use fmod from <math.h> and link with -lm on Unix systems.

💡 Tip: once this works, try these classic % applications: (a) extract digits from a number (n % 10 gives the last digit), (b) check even/odd (n % 2), (c) convert seconds to hours/minutes/seconds using repeated / and %, (d) implement a circular buffer index that wraps (i % capacity). Each is a small extension of the same operator.

📖 Related Tutorials

  • Previous: Divide Two Numbers Using the '/' Operator
  • Next: Bitwise AND on Two Numbers Using '&'
  • All 15 Operator Assignments in C
  • Arithmetic Operators in C
  • Operators and Expressions
  • Bitwise Operators in C
  • Operators FAQ

Previous Topic: -->> 4. Divide Two Numbers Using the '/' Operator   ||   Next Topic: -->> 6. Bitwise AND on Two Numbers Using the '&' Operator


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.