|
Contact Tekstart to get answers to the questions |
Repetition - while
statement |
Calculate the sum of numbers from 1 to 10. |
n=1
t=0
while n<=10:
......
......
print(t) |
Calculate the sum of odd numbers from 1 to 10. |
n=1
t=0
while n<=10:
if ......:
t+=......
n+=1
print(t) |
Calculate the sum of even numbers from a user given range. |
start=int(input('Enter start number: '))
stop=int(input('Enter stop number: '))
t=0
n=start
while .......:
if n%2==0:
......
n+=1
print('Sum of even numbers from '+str(start)+' to '+str(stop)+' is '+str(t)) |
Input a series of numbers (one at a time) ending with -99 and output the
sum of positive odd numbers. |
t=0
num=int(input('Enter a number: '))
while num!=-99:
if ...... and ......:
t+=num
num=int(input('Enter a number: '))
print(t)
|
Input a series of numbers (one at a time) ending with -99 and output all
negative numbers as a list (except -99). |
nlist=[]
num=int(input('Enter a number: '))
while num!=-99:
if ........:
nlist.append(......)
num=int(input('Enter a number: '))
print(nlist)
|
Input a series of numbers (one at a time) ending with -99 and output the
average of positive numbers. |
t=0
c=0
num=int(input('Enter a number: '))
while num!=-99:
if num>0:
t+=.....
c+=.....
num=int(input('Enter a number: '))
avg=......
print(avg) |
Input n number of numbers (one at a time) and output the sum of positive
even numbers. |
t=0
c=0
n=int(input('How many numbers: '))
while c<n:
num=int(input('Enter a number: '))
if num>0 and ........:
t+=......
c+=.....
print(t)
|