0

How can plus 1 to 100 odd number (use for i in range(1,101))

sum=0 for i in range(1,101): sum=sum+i

25th Mar 2017, 2:29 PM
ģ •ģ€ķ¬
ģ •ģ€ķ¬ - avatar
6 Answers
+ 2
sum([i for i in range(1, 101) if i % 2])
25th Mar 2017, 2:42 PM
Moataz El-Ibiary
Moataz El-Ibiary - avatar
+ 2
use if(i%2 !=0) sum = sum + i; @abhishek
25th Mar 2017, 2:46 PM
Mayur Chaudhari
Mayur Chaudhari - avatar
+ 1
for (i=1;i<=100;i++) { if (i%2!=0) sum=sum+i; } This code is for C language
25th Mar 2017, 2:38 PM
Abhishek Arora
Abhishek Arora - avatar
+ 1
is it ok to use (i%2==1) instead of (i%2!=0)??
25th Mar 2017, 2:48 PM
ģ •ģ€ķ¬
ģ •ģ€ķ¬ - avatar
+ 1
print(sum( [ i for i in range(1,100,2)]))
25th Mar 2017, 3:00 PM
SAMI Awad
SAMI Awad - avatar
0
sum = 0 for i in range(1,101): if (i % 2 != 0): sum = sum + i print(sum)
25th Mar 2017, 2:43 PM
Mayur Chaudhari
Mayur Chaudhari - avatar