Find the output of the following program segments:
(i) a = 110
while a > 100:
print(a)
a -= 2
(ii) for i in range(20,30,2):
print(i)
(iii) country = 'INDIA'
for i in country:
print (i)
(iv) i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i = i + 2
print (sum)
(v) for x in range(1,4):
for y in range(2,5):
if x * y > 10:
break
print (x * y)
(vi) var = 7
while var > 0:
print ('Current variable value: ', var)
var = var -1
if var == 3:
break
else:
if var == 6:
var = var -1
continue
print ("Good bye!")
Answers
Answered by
4
Answer:
hope it is helpful make me as branlist....
Attachments:
Answered by
1
Answer:
(i) Output: 110, 108, 106, 104, 102
(ii) Output : 20 22 24 26 28
(iii) Output : I N D I A
(iv) Output : 12
(v) Output : 12
(vi) Current variable value: 7
Current variable value: 5
Current variable value: 4
"Good bye!"
Explanation:
(i)a = 110
while a > 100:
print(a)
a -= 2
- Initial value of a is 110;
- Inside the while loop , the print statement will print 110 and then the value of a will be reduced by 2 (a-=2);
- Again the while loop will execute and will print 108 followed by reduction of the value of a by 2.
- this while loop will execute till the value of a is 102.
- Hence the output is : 110, 108, 106, 104, 102
(ii) for i in range(20,30,2):
print(i)
- Starting value of i = 20;
- Maximum value of i = 30;
- Every time the loop will execute, value of i will be incremented by 2.
- So, the output will be : 20 22 24 26 28
(iii) country = 'INDIA'
for i in country:
print (i)
- print (i) will print all the character that is present in the value of the string 'country'.
(iv) i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i = i + 2
print (sum)
- This set of code will print the value of 'sum' . 'sum' stores the sum of all values between 0 to 8 that are divisible by 4.
- Between 0 to 8 , 4 and 8 are two numbers which are divisible by 4.
- Hence the output is 4+8 = 12.
(v) for x in range(1,4):
for y in range(2,5):
if x * y > 10:
break
print (x * y)
- This set of code has nested for loop. For x=3 and y=4 , x*y = 12 which is greater than 10.
- Once we get a value of x * y that is greater than 10, break statement will bring the execution control out of the loop and print statement will print the value of x*y i.e. 12.
(vi) var = 7
while var > 0:
print ('Current variable value: ', var)
var = var -1
if var == 3:
break
else:
if var == 6:
var = var -1
continue
print ("Good bye!")
- first the value of var = 7 will be printed by print ('Current variable value: ', var) statement.
- then value of var will be 6 and 'if var == 6: var = var -1' will execute and value of var will be 5 and will be printed.
- Again var will be decremented and var=4 will be printed .
- Next var=3 and 'if var == 3: break' will execute; 'break statement will bring the execution control to print ("Good bye!") and it will print "Good bye!".
- Hence, outputs are:
- Current variable value: 7
- Current variable value: 5
- Current variable value: 4
- "Good bye!"
Similar questions
Hindi,
5 months ago
Science,
5 months ago
Business Studies,
10 months ago
Computer Science,
10 months ago
Math,
1 year ago