modulus in c

Posted under C++ Course On By mohammed.mutawe

modulus in c

C++ language uses remainder equations extensively, specifically in complex mathematics algorithms, and they are not dispensed with in graphics processing and search engines. Basically, the remainder ratio is based on verifying the acceptance of dividing one number by another, and what is the surplus or remainder of the division between the two numbers.

 

Modulus

If we suppose that we want to divide two integers, for example: 4 divided by 2, then the remainder of the division in this case is equal to zero, because the number 4 is a multiple of 2 and there is no remainder to divide between them.

If we reverse the process so that 2 is divided by 4, and because the number 2 is not divisible by 4 in the integers, then the remainder of the division is 2 because the process of division in the integers is not possible.

 

In C++

The remainder is denoted in C++ by the symbol %, which is a percentage sign in general and modulus in C language, and now we will prepare two variables of type integer and put the value of the remainder inside another variable of the same type.

Please, follow these steps with us:

  • We define two variables and put a value in each variable, let a = 5 and b = 13, as follows:
#include<iostream>
using namespace std;


int main() {

    int a = 5;
    int b = 13;


    return 0;
}

 

  • Then we add a variable to save the remainder of the division when it is executed:
#include<iostream>
using namespace std;


int main() {

    int a = 5;
    int b = 13;
    int mod = a % b;


    return 0;
}
  • And we will display the value of the remainder after putting it in the mod variable, as follows:
#include<iostream>
using namespace std;


int main() {

    int a = 5;
    int b = 13;
    int mod = a % b;


    std::cout << "a modulas b =  " << mod << endl;




    return 0;
}
  • After executing the following code, the remainder of the division is 5.

 

3.Example

Please, to understand Modulus well, copy the following code into your code editor.

#include<iostream>
using namespace std;


int main() {

    int a = 5;
    int b = 13;
    int mod = a % b;


    std::cout << "value of a =  " << a << endl;
    std::cout << "value of b =  " << b << endl;
    std::cout << "a modulas b =  " << mod << endl;




    return 0;
}

 

Leave a comment

Your email address will not be published. Required fields are marked *