How to Compute Remainders in C++

All the programming languages support every kind of arithmetic operation. The only thing that is tricky is how to implement these operations. Some of the arithmetic operations are easy to code and some are difficult, but easy does not mean that they are not tricky. One such tricky arithmetic operation is computing remainders. For instance, if you divide 11 by 2 what will be the answer in the form of the remainder? Obviously the remainder will be 1. But how can you calculate this? I will tell you simple instructions to compute the remainder in any language, but I will be specifically coding C++ in this tutorial. Procedure remains the same.

Instructions

  • 1

    If you want to compute the remainder of some division then the way to calculate this is using modulus operator. The modulus operator we use is the percentage (%) sign. As when we use the simple division (/) operator it tells us the answer of the division by truncating the figures after point but modulus tells us the remainder if division. The mentioned example will demonstrate to you how you can do this.

  • 2

    Example: The following example uses modulus operation to tell you that if a number is odd or even.

    #include
    using namespace std;
    int main()
    {
    int test_no;
    cout<<”Please enter the number to check : ” cin >> test_no;
    //once the number is entered the code will take the remainder of the number after dividing it by 2
    if ( test_no % 2 != 0 )
    {
    cout << test_no << " is odd "<
    }
    else if(test_no % 2==0){
    cout << test_no << " is even "<
    }

    I hope that this tutorial will help you to understand the modulus operation and its use in C++.

  • 3

    If you have any problems with these steps then it might be a good idea for you to repeat the instructions and start again. Take your time and try not to skip anything important to avoid any issues. However, if you find that you are still having trouble then go online and visit one of the many different websites or forums that deal with the C++ programming language. Look for solutions to your problems as other users might also experience the same issues. There are many expert and professional users that are always willing to help you out. Be sure to post any queries or questions that you might have on a forum for some extended help.

  • 4

    You can also take a quick trip to your local library or bookstore and pick up some nice books on C++ programming language. You will find tons of books on the subject and just study a few books for some time until you are able to understand the flexibility of coding. Remember some of these programming languages can be very tricky if you are not that experienced, so take your time and research or study as much as you can to gain some insight.

Leave a Reply

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


2 × = eight