Why the output is 10 in print(1+2+3+4%5)? | SoloLearn: Learn to code for FREE!

+2

Why the output is 10 in print(1+2+3+4%5)?

10/20/2020 1:51:27 PM

[email protected] [INACTIVE]

26 Answers

New Answer

+8

As Abhay already said "%" have higher precedence that "+" operator so expression having "%" will be evaluated first. 1+2+3+4%5 = 1+2+3+4 { as 4%5 = 4} = 10 In SMMR 's answer (1+2+3+4) will be evaluated first as parentheses "( )" have got even higher precedence than "%". (1+2+3+4)%5 = 10%5 = 0 You can know about precedence of different operators here👇 https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html

+11

4%5 returns 4 , % has higher precedence than + so it is evaluated first

+7

Keshav Kumar the modulo "%" operator returns the remainder after the division. In your case 4/5 will yield 0 as quotient and 4 will be the remainder. Thus 4%5 = 4

+6

Arsenic I got what u mean But how 4%5 will be evaluated as 4?🤔

+5

1) 4%5 = 4 # Because the division of the moddudel is the first to add up and subtract=> 2)print(1+2+3+4)=10

+5

‎יהודי‎ I like your creative spelling of modulo.

+5

‎יהודי‎ I like your creative spelling of modulo. < Thank you for supporting me.

+4

SMMR then it will comes out to be 0

+4

SMMR i don't expected any answer. I don't know what will be the answer and i got confused

+4

SMMR i don't have any purpose i just found this question in Sololearn challenge mode

+4

SMMR ofc Python challenge

+4

Arsenic thanks alot bro

+4

Rutvikkumar Gondaliya Sanjay Kamath thanks 😊

+4

The output of this is based on Mathematics % has higher precedence than + So 4%5 Means quotient is 0 and remainder is 4 Then adding it to the rest 1+2+3+4 = 10 Thus, problem is solved

+3

SMMR That's all the question is nothing more

+3

Precedence: #1: % #2:+/- hence : result = 1+2+3+(4%5) = 10 (4 mod 5 evaluated first)

+3

4%5 = 4 so, 1+2+3+4%5 = 10 because more priority % operator then + operator. (1+2+3+4)%5 = 0 because '()' <-- curve bracket use in equation so first of all sum of 1 to 4 and then modulo operator work. Okay this reason to answer is 10.

+2

What is the expected output?

+2

Probably this will help print((1+2+3+4)%5)

+2

You could use bracket() any where you want