Python R4.2 Write a loop that computes a. The sum of all even numbers between 2
ID: 3881131 • Letter: P
Question
Python R4.2 Write a loop that computesa. The sum of all even numbers between 2 and 100 (inclusive).
b. The sum of all squares between 1 and 100 (inclusive).
c. The sum of all odd numbers between a and b (inclusive).
d. The sum of all odd digits of n. (For example, if n is 32677, the sum would be 3 + 7 + 7 = 17.)
Python R4.2 Write a loop that computes
a. The sum of all even numbers between 2 and 100 (inclusive).
b. The sum of all squares between 1 and 100 (inclusive).
c. The sum of all odd numbers between a and b (inclusive).
d. The sum of all odd digits of n. (For example, if n is 32677, the sum would be 3 + 7 + 7 = 17.)
R4.2 Write a loop that computes
a. The sum of all even numbers between 2 and 100 (inclusive).
b. The sum of all squares between 1 and 100 (inclusive).
c. The sum of all odd numbers between a and b (inclusive).
d. The sum of all odd digits of n. (For example, if n is 32677, the sum would be 3 + 7 + 7 = 17.)
R4.2 Write a loop that computes
a. The sum of all even numbers between 2 and 100 (inclusive).
b. The sum of all squares between 1 and 100 (inclusive).
c. The sum of all odd numbers between a and b (inclusive).
d. The sum of all odd digits of n. (For example, if n is 32677, the sum would be 3 + 7 + 7 = 17.)
Explanation / Answer
Part a)
sum=0
for i in range(2,101,2):
sum+=i
Part b)
sum=0
for i in range(1,11,1):
sum+=i*i
Part c)
def func(a,b):
sum=0
if (a%2==0):
a=a+1
if(b%2==1):
b=b+1
for i in range(a,b+1,2):
sum+=i
print(sum)
Part d)
def digitsum(n):
sum=0
while(n):
dig=n%10
if(dig%2==1):
sum+=dig
n=(n-dig)/10
print(sum)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.