The modulo operator in C will give the remainder that is left over when one number is divided by another. For example, 23 % 4 will result in 3 since 23 is not evenly divisible by 4, and a remainder of 3 is left over.

If you want to output whether or not a number is divisible by 4, you need to output something other than just the mod result. Essentially, if mod = 0 than you know that one number is divisible by another.

If you want to output whether or not the number is divisible by 4, I would suggest creating a new character that is set to "y" (yes) or "n" (no) depending on the result of the mod operation. Below is one possible implementation to generate a more meaningful output:

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

int main()
{
    int my_input[] = {23, 22, 21, 20, 19, 18};
    int n, mod;
    char is_divisible;
    int nbr_items = sizeof(my_input) / sizeof(my_input[0]);

    for (n = 0; n < nbr_items; n++)
    {
        mod = my_input[n] % 4;
        is_divisible = (mod == 0) ? 'y' : 'n';
        printf("%d modulo %d --> %c\n", my_input[n], 4, is_divisible);
    }
}

This will give the following:

23 modulo 4 --> n
22 modulo 4 --> n
21 modulo 4 --> n
20 modulo 4 --> y
19 modulo 4 --> n
18 modulo 4 --> n
Answer from rakeshb on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ modulo-operator-in-c-cpp-with-examples
Modulo Operator (%) in C/C++ with Examples - GeeksforGeeks
July 12, 2025 - In C or C++, the modulo operator (also known as the modulus operator), denoted by %, is an arithmetic operator. The modulo division operator produces the remainder of an integer division which is also called the modulus of the operation.
Top answer
1 of 2
9

The modulo operator in C will give the remainder that is left over when one number is divided by another. For example, 23 % 4 will result in 3 since 23 is not evenly divisible by 4, and a remainder of 3 is left over.

If you want to output whether or not a number is divisible by 4, you need to output something other than just the mod result. Essentially, if mod = 0 than you know that one number is divisible by another.

If you want to output whether or not the number is divisible by 4, I would suggest creating a new character that is set to "y" (yes) or "n" (no) depending on the result of the mod operation. Below is one possible implementation to generate a more meaningful output:

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

int main()
{
    int my_input[] = {23, 22, 21, 20, 19, 18};
    int n, mod;
    char is_divisible;
    int nbr_items = sizeof(my_input) / sizeof(my_input[0]);

    for (n = 0; n < nbr_items; n++)
    {
        mod = my_input[n] % 4;
        is_divisible = (mod == 0) ? 'y' : 'n';
        printf("%d modulo %d --> %c\n", my_input[n], 4, is_divisible);
    }
}

This will give the following:

23 modulo 4 --> n
22 modulo 4 --> n
21 modulo 4 --> n
20 modulo 4 --> y
19 modulo 4 --> n
18 modulo 4 --> n
2 of 2
7

I'm sure we know the basic division equation from high school math

dividend = divisor*quotient + remainder

Now:
1. The "/" operator gives us the quotient.
2. The "%" operator gives us the remainder

example:

    say a = 23, b = 4
    a / b = 23 / 4 = 5
    a % b = 23 % 4 = 3

    23 = 4*5 + 3 

Here 4 is the quotient and 3 is the remainder.

If a number is perfectly divisible by a divisor, then remainder is zero.

So:

    20/4 = 5 (quotient)
    20%4 = 0 (remainder)

To test if a no if divisible by 4, the check should be something like if (num % 4 == 0).
Hope this helps!

People also ask

In what cases do we get an error from the compiler when using the modulo operator?
If p and q are integers, then for p % q: If p is 0, then division by zero generates a compile-time error. If p is some number (integer) and q is 0, then also we get a compile-time error. Also, one cannot use and apply the % operator on an available set of floating numbers (that is float or double). If we try to use this operator with any floating-point variables or even constants, the compiler will generate a compile-time error.
๐ŸŒ
byjus.com
byjus.com โ€บ gate โ€บ modulus-operator-in-c
Syntax of Modulus Operator in C
How does the % operator work?
The working of this kind of operator happens on the basis of the value that the end-user receives since it always finds a remainder of the two available numbers/ integers with respect to the numerator and denominator. Remember that we are talking about remainder and not a quotient of the numerator-denominator pair. For example, 9 % 2 will give us 1 as a remainder. It is because when we divide 9 by 2, we get 3 as a quotient, but there remains a remainder of 1.
๐ŸŒ
byjus.com
byjus.com โ€บ gate โ€บ modulus-operator-in-c
Syntax of Modulus Operator in C
๐ŸŒ
Ucdavis
scalettar.physics.ucdavis.edu โ€บ cosmos โ€บ modulo.pdf pdf
C PROGRAMMING: INTEGER DIVISION AND MODULO (%)
C PROGRAMMING: INTEGER DIVISION AND MODULO (%) When two integers are divided, the result is truncated. That is, when the computer calculates ยท 23/4, instead of getting 5.75 it gets 5. The computer literally asks how many times 4 goes ยท into 23, and doesnโ€™t care anything about the remainder.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 989220 โ€บ modulo-and-integer-division-in-c-and-c-languages
Modulo and integer division in C++ and C languages. | Sololearn: Learn to code for FREE!
See: https://stackoverflow.com/questions/24074869/why-is-the-behavior-of-the-modulo-operator-different-between-c-and-ruby-for ... -- found this which maybe of interest. Originally, MOD(n, m) was defined only for positive values of both m and n, and leaves the result to be implementation-dependent when either of m or n is negative. ... In Mathematics we have : Dividend = Divisor * Quotient + Remainder so according to your words :) : In C++ we have: (-17) = (+5) * (-3) + (-2) => (-17) = (-15) + (-2) =>(-17) = (-17) => So its correct :) In Python we have : (-17) = (+5) * (-4) + (+3) => (-17) = (-20) + (+3) =>(-17) = (-17) => So its correct :) So don't say Mathematics and Python says other answer :) because both of them (C++ & Python) are correct just having different processes :)
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ differentiate-the-modulo-and-division-by-using-c-programming-language
Differentiate the modulo and division by using C Programming language?
Modulo โˆ’ Represents as % operator. And gives the value of the remainder of an integer division. Division โˆ’ represents as / operator. And gives the value of the quotient of a division. #include<stdio.h> int main(){ int a,b,c; printf("enter a,b,c values:"); scanf("%d%d%d,&a,&b,&c); printf("a/b=%d ...
๐ŸŒ
BYJUS
byjus.com โ€บ gate โ€บ modulus-operator-in-c
Syntax of Modulus Operator in C
November 9, 2022 - The modulus operator is added in the arithmetic operators in C, and it works between two available operands. It divides the given numerator by the denominator to find a result. In simpler words, it produces a remainder for the integer division.
Find elsewhere
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ modulo in c
The Modulo Operator in C | Delft Stack
March 12, 2025 - The modulo operator, often represented by the percentage symbol (%), is a fundamental tool in C programming. It allows developers to determine the remainder of a division operation between two integers.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Modulo
Modulo - Wikipedia
6 days ago - In mathematics, the result of the modulo operation is an equivalence class, and any member of the class may be chosen as representative; however, the usual representative is the least positive residue, the smallest non-negative integer that belongs to that class (i.e., the remainder of the Euclidean division).
๐ŸŒ
Quora
quora.com โ€บ What-is-modulus-in-C-programming
What is modulus in C programming? - Quora
Answer (1 of 3): Modulus is one of the arithmetic operators in C. It is used to find the the remainder during a division operation. Example, division - 4 divided by 2 gives 2(quotient for 4/2, 4*2+0) and modulus - 4 modulus 2 gives 0(remainder ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ modulus operator in c
What is Modulus Operator in C? - Scaler Topics
December 19, 2022 - You may think how the result is 4 in the second printf() statement, let's understand this by breaking it down using simple division law 4 % 5 should give the remainder, when 4 is divided by 5, but as 4 is smaller than 5, so it will take 0 as the possible quotient, and hence will break as - This is why we will have 4 as the remainder in the second case
๐ŸŒ
Cprogramming
cprogramming.com โ€บ tutorial โ€บ modulus.html
Modulus Operator in C and C++ - Programming Tutorials - Cprogramming.com
How to begin Get the book ยท C tutorial C++ tutorial Game programming Graphics programming Algorithms More tutorials
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ modular-division
Modular Division - GeeksforGeeks
July 14, 2025 - Unlike regular arithmetic, modular systems do not support direct division. Instead, division is performed by multiplying the dividend by the modular multiplicative inverse of the divisor under a given modulus.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [c++] can someone explain modulus operator(%)?
r/learnprogramming on Reddit: [C++] Can someone explain Modulus operator(%)?
October 20, 2015 -

I started learning C++ a few days ago. I've learned the basics for now. +,-.*,/ . But I can't properly understand the modulus operator. I know that if we take modulus of a number by 10, it will give us the last digit of that number. For example:

int num=12345;
num%10;    ----> 5

Now the problem is I can't understand this code. The question is:

  • A cashier has currency notes of denominations 10, 50 and 100. If the amount to be withdrawn is input through the keyboard in hundreds, find the total number of currency notes of each denomination the cashier will have to give to the withdrawer. i.e A user input 480 then the answer will be 4 notes of Rs. 100, one note of Rs. 50 and 3 notes of Rs. 10.

and the code is:

#include<stdio.h>
#include<conio.h>
void main(void){
int amount,t,f,h,c=0;
clrscr();
printf("Enter total amount:");
scanf("%d",&amount);
h=amount/100;
**f=(amount%100)/50;  //line A
t=((amount%100)%50)/10;   //line B
c=(((amount%100)%50)%10);**  //line C
printf("\n\n100 Rs notes:%d",h);
printf("\n50 Rs notes:%d",f);
printf("\n10 Rs notes:%d",t);
printf("\nCoins remaining:%d",c);
getch();
}

can someone explain why we did what we did in lines A,B,C?

๐ŸŒ
Lucidar
lucidar.me โ€บ en โ€บ c-class โ€บ lesson-03-02-modulo
Lesson 3.2. modulo | Lulu's blog
Learn C programming language. Lesson 3.2. The modulo in C, the modulo is the remainder of the integer division, denoted by %, is an arithmetic operator
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-using-a-modulo-operator-and-division-by-its-inverse-in-programming-languages-Is-there-a-practical-reason-for-choosing-one-over-the-other
What is the difference between using a modulo operator and division by its inverse in programming languages? Is there a practical reason for choosing one over the other? - Quora
Answer (1 of 2): The problem with division (and modulo) is that they are often one of the slowest operations that can be performed on a processor. If youโ€™re working with processors for embedded systems, thereโ€™s sometimes hardware for making multiplication in just over the same time it takes to ma...
๐ŸŒ
Rebus Community
press.rebus.community โ€บ programmingfundamentals โ€บ chapter โ€บ integer-division-and-modulus
Integer Division and Modulus โ€“ Programming Fundamentals
December 15, 2018 - Want to create or adapt books like this? Learn more about how Pressbooks supports open publishing practices. ... In integer division and modulus, the dividend is divided by the divisor into an integer quotient and a remainder.
๐ŸŒ
Reddit
reddit.com โ€บ r/cs50 โ€บ can someone explain the modulo operation to me like i'm five? (credit.c)
r/cs50 on Reddit: Can someone explain the modulo operation to me like I'm five? (Credit.c)
April 24, 2020 -

So I'm really struggling with the credit problem in pset1. I'm completely new to coding and computer science and everything but I really want to try doing all of the problems in this class. I had to look up mario and cash to gain some insight into what I've been doing wrong when I approach a problem. With each completed problem the pieces come together in my head a little more than they were before.

Credit has me totally through a loop. It seems like for all of the problems I understand what to use in C as far as loops and what have you but I really struggle with figuring out what like operations I'm supposed to use to tell the program exactly what to do. With the whole modulo operation thing I don't really understand what it's doing and what I have to do to get it to give me the numbers I need. Something tells me I need to use different powers of ten to somehow separate the full credit card number into individual digits but because I don't understand what % 10 is actually doing I can't fully figure out what I need to do.

I'm struggling with trying to decide if I should just skip this problem and try again another time or if I should contemplate it more. With each problem it seems like I struggle with like some kind of mathematical aspect, I feel like there's something that I'm missing but I can't tell if I just how to make myself think about things differently. I feel like I'm making myself crazy about this lol

๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ examples โ€บ remainder-quotient
C Program to Compute Quotient and Remainder
Similarly, the remainder is evaluated using % (the modulo operator) and stored in remainder. remainder = dividend % divisor; Finally, the quotient and remainder are displayed using printf(). printf("Quotient = %d\n", quotient); printf("Remainder = %d", remainder); Learn more about how operators ...
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ c-use-the-modulus-operator-in-c-435358
How to Use the Modulus Operator in C | LabEx
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. 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.