How does the modulus operator work?

前端 未结 5 1693
忘了有多久
忘了有多久 2020-11-27 17:19

Let\'s say that I need to format the output of an array to display a fixed number of elements per line. How do I go about doing that using modulus operation?

Using C

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 18:02

    Basically modulus Operator gives you remainder simple Example in maths what's left over/remainder of 11 divided by 3? answer is 2

    for same thing C++ has modulus operator ('%')

    Basic code for explanation

    #include 
    using namespace std;
    
    
    int main()
    {
        int num = 11;
        cout << "remainder is " << (num % 3) << endl;
    
        return 0;
    }
    

    Which will display

    remainder is 2

提交回复
热议问题