0
What is the important of modulus % in c++
2 Réponses
+ 2
the modulus % is used to get the the remainder of a division.
Some usage of it includes
You have a money value in pennies
Int moneyPenny = 312
If you want to show the value in the form of dollars you can divide by a hundred but since this is integer division then it will return only 3
Here comes the modulus to help get the remainder which is 12
Then using string concatenation "quot; + value1 + "." + value2
Another example is if you want to know if a number is even or odd, Say for example we get user input and want to check if a number is even or odd the use modulus 2 and if the value is zero then it is even if the value is one then the number is odd.
Code example in java
Hope that helped!
Give a like if that helped.
0
Asking this question is like asking what is the importance of the division / in C++
A really basic example of when you would use it say you wanna check if the number is an even or an odd number, you would do:
num = 2
if (num % 2 == 0) {
std::cout << "Even!" << std::endl;
}