I had the problem to reverse the digits and I searched and found the answer, but I was wondering as to what is the fuction of the "%"
I was playing with it when changing values of 10 to 100 and 1, but is it a math formula or something else?

Dani AI

Generated

and are on the right track. A concise way to think about % in C/C++ is as the integer remainder operator: for integers a and b the remainder r can be written as r = a - (a / b) * b, where / is integer division. That identity explains the behavior you saw when changing 10, 100 or 1: using 10 isolates the last decimal digit, 100 the last two, and modulo 1 always yields 0 because every integer divides evenly by 1. See the C/C++ operator reference for the precise language rules and corner cases: cppreference — arithmetic operators (remainder).

Practical note for reversing digits (in plain steps, no duplicate code from the thread): repeatedly take the last digit by modulo base 10, append it to an accumulated result by multiplying that result by 10 and adding the digit, then remove the last digit with integer division. Repeat until the source value is exhausted. Check for overflow before multiplying the accumulator by 10.

Important caveats: the sign of the remainder for negative operands follows the language rules (in modern C/C++ the remainder has the sign of the dividend); division or remainder by zero is undefined; % is defined for integers only — use fmod for floating-point remainders. Also watch mixed signed/unsigned operands, which cause implicit conversions. See the library reference for fmod and more details: cppreference — fmod.

In short: the operator is not a mysterious magic function — it computes the integer leftover after division, and that property is why it is useful for digit extraction and cyclic arithmetic.

Recommended Answers

All 2 Replies

% is the Modulo function. It gives you the remainder of a division between two numbers. Its an operator in C and C++.

% is modulo division operator in c. it works on two numbers and evalutes the remaining value after dividing.
for example 10%3 = 1
10%4 = 2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.