Find the division remainder of a number

前端 未结 12 1198
刺人心
刺人心 2020-11-28 07:30

How could I go about finding the division remainder of a number in Python?

For example:
If the number is 26 and divided number is 7, then the division remainder

相关标签:
12条回答
  • 2020-11-28 08:10

    If you want to avoid modulo, you can also use a combination of the four basic operations :)

    26 - (26 // 7 * 7) = 5
    
    0 讨论(0)
  • 2020-11-28 08:11

    you are looking for the modulo operator:

    a % b
    

    for example:

    26 % 7
    

    Of course, maybe they wanted you to implement it yourself, which wouldn't be too difficult either.

    0 讨论(0)
  • 2020-11-28 08:13

    If you want to get quotient and remainder in one line of code (more general usecase), use:

    quotient, remainder = divmod(dividend, divisor)
    #or
    divmod(26, 7)
    
    0 讨论(0)
  • 2020-11-28 08:13

    You can find remainder using modulo operator Example

    a=14
    b=10
    print(a%b)
    

    It will print 4

    0 讨论(0)
  • 2020-11-28 08:17

    Modulo would be the correct answer, but if you're doing it manually this should work.

    num = input("Enter a number: ")
    div = input("Enter a divisor: ")
    
    while num >= div:
        num -= div
    print num
    
    0 讨论(0)
  • 2020-11-28 08:20

    26 % 7 (you will get remainder)

    26 / 7 (you will get divisor can be float value )

    26 // 7 (you will get divisor only integer value) )

    0 讨论(0)
提交回复
热议问题