+ 3

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

20th Oct 2020, 1:51 PM
‎Keshav
‎Keshav - avatar
25 Answers
+ 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
21st Oct 2020, 5:41 AM
Arsenic
Arsenic - avatar
+ 11
4%5 returns 4 , % has higher precedence than + so it is evaluated first
20th Oct 2020, 1:53 PM
Abhay
Abhay - avatar
+ 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
21st Oct 2020, 6:50 AM
Arsenic
Arsenic - avatar
+ 6
Arsenic I got what u mean But how 4%5 will be evaluated as 4?🤔
21st Oct 2020, 5:48 AM
‎Keshav
‎Keshav - avatar
+ 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
22nd Oct 2020, 7:02 AM
‎ Герман Папанов(הרמן פפנוב)‎
‎ Герман Папанов(הרמן פפנוב)‎ - avatar
+ 5
‎יהודי‎ I like your creative spelling of modulo.
22nd Oct 2020, 10:22 AM
Sonic
Sonic - avatar
+ 5
‎יהודי‎ I like your creative spelling of modulo. < Thank you for supporting me.
22nd Oct 2020, 10:41 AM
‎ Герман Папанов(הרמן פפנוב)‎
‎ Герман Папанов(הרמן פפנוב)‎ - avatar
+ 4
SMMR then it will comes out to be 0
21st Oct 2020, 1:57 AM
‎Keshav
‎Keshav - avatar
+ 4
SMMR i don't expected any answer. I don't know what will be the answer and i got confused
21st Oct 2020, 5:53 AM
‎Keshav
‎Keshav - avatar
+ 4
SMMR i don't have any purpose i just found this question in Sololearn challenge mode
21st Oct 2020, 5:56 AM
‎Keshav
‎Keshav - avatar
+ 4
SMMR ofc Python challenge
21st Oct 2020, 5:57 AM
‎Keshav
‎Keshav - avatar
+ 4
Arsenic thanks alot bro
21st Oct 2020, 6:53 AM
‎Keshav
‎Keshav - avatar
22nd Oct 2020, 5:05 AM
‎Keshav
‎Keshav - avatar
+ 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
22nd Oct 2020, 6:35 AM
Ayush Vats
Ayush Vats - avatar
+ 3
SMMR That's all the question is nothing more
21st Oct 2020, 5:59 AM
‎Keshav
‎Keshav - avatar
+ 3
Precedence: #1: % #2:+/- hence : result = 1+2+3+(4%5) = 10 (4 mod 5 evaluated first)
22nd Oct 2020, 3:18 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 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.
22nd Oct 2020, 4:52 AM
Rutvikkumar Gondaliya
Rutvikkumar Gondaliya - avatar
+ 2
What is the expected output?
20th Oct 2020, 7:47 PM
SMMR
SMMR - avatar
+ 2
Probably this will help print((1+2+3+4)%5)
20th Oct 2020, 7:49 PM
SMMR
SMMR - avatar
+ 2
You could use bracket() any where you want
21st Oct 2020, 4:14 AM
SMMR
SMMR - avatar