NCERT Computer Science Class #11 – Chapter #6 Solutions – Q #5

Chapter #6 – Flow of Control

NCERT Computer Science Chapter 6 Solutions Q 5

Class 11 – Chapter 6 Solutions – Q 5 – Find the output of the following program segments:

(i) Give output of the following:

a = 110

while a > 100:
    print(a)
    a -= 2

Explanation of the Code



Initialization: The variable a is initialized to 110.

While Loop: The loop continues as long as a is greater than 100.

Print Statement: Inside the loop, it prints the current value of a.

Decrement: After printing, a is decreased by 2.

Output

110
108
106
104
102

(ii) Give output of the following:

for i in range(20, 30, 2): 
    print(i)

Explanation of the Code

1.	range(20, 30, 2):
        The range() function generates a sequence of numbers.
	It starts at 20 and goes up to (but does not include) 30, incrementing by 2       
        each time.
2.	for i in ...:
	This loop iterates over each number generated by range(), assigning it to 
        the variable i.
3.	print(i):
	Inside the loop, it prints the current value of i.

Output

When you run this code, the output will be:

20
22
24
26
28

(iii) Give output of the following:

country = 'INDIA' 
for i in country: 
    print(i)

Explanation of the Code

1.	Variable Assignment: The variable country is assigned the string 'INDIA'.
2.	for i in country::
	This loop iterates over each character in the string country, assigning 
        each character to the variable i in each iteration.
3.	print(i):
	Inside the loop, it prints the current character stored in i.

Output

When you run this code, the output will be:

I
N
D
I
A

(iv) Give output of the following:

i = 0
sum = 0 

while i < 9: 
    if i % 4 == 0: 
        sum = sum + i 
    i = i + 2 

print(sum)

Explanation of the Code:

1.	Initialization:
	i is initialized to 0.
	sum is initialized to 0.
2.	While Loop:
	The loop continues as long as i is less than 9.
3.	If Condition:
	Inside the loop, there's an if statement that checks if i is divisible by 4 
        (i % 4 == 0).
	If true, it adds the value of i to sum.
4.	Incrementing i:
	Regardless of whether the condition is met, i is incremented by 2 in each 
        iteration.
5.	Print Statement:
	After the loop finishes, it prints the total value of sum.

Output :

When you run this code, the output will be:

12

Explanation of Output:

      •	The values of i during the iterations will be:
      •	First iteration: i = 0, which is divisible by 4, so sum = 0 + 0 = 0.
      •	Second iteration: i = 2, not divisible by 4, so sum remains 0.
      •	Third iteration: i = 4, which is divisible by 4, so sum = 0 + 4 = 4.
      •	Fourth iteration: i = 6, not divisible by 4, so sum remains 4.
      •	Fifth iteration: i = 8, divisible by 4, so sum = 4 + 8 = 12
The loop stops when i reaches 10, which is not less than 9.

(v) Give output of the following:

for x in range(1,4):

     for y in range(2,5):

          if x * y > 10:

               break

          print (x * y)

Explanation of the Code

1.	Outer Loop (for x in range(1, 4)):
      •	x will take values 1, 2, and 3.
2.	Inner Loop (for y in range(2, 5)):
      •	For each value of x, y will take values 2, 3, and 4.
3.	Condition Check (if x * y > 10):
      •	If the product of x and y exceeds 10, the inner loop will break.
4.	Print Statement:
      •	If the product is less than or equal to 10, it will be printed.

OUTPUT:

2
3
4
4
6
8
6
9

(vi) Give output of following:

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!")

Explanation of the Code Execution

1.	Initialization: The variable var is initialized to 7.
2.	While Loop: The loop continues as long as var is greater than 0.
3.	First Iteration (var = 7):
      •	Prints: Current variable value: 7
      •	Decrements var to 6.
      •	Since var is 6, it enters the inner condition, decrements var to 5, and 
        continues to the next iteration without executing the print statement for 
        "Good bye!".
4.	Second Iteration (var = 5):
      •	Prints: Current variable value: 5
      •	Decrements var to 4.
      •	Neither condition for breaking nor continuing applies, so it prints: 
        "Good bye!".
5.	Third Iteration (var = 4):
      •	Prints: Current variable value: 4
      •	Decrements var to 3.
      •	Since var is now 3, the loop breaks, and it does not print "Good bye!" 
        this time.

OUTPUT:

Current variable value: 7
Current variable value: 5
Good bye!
Current variable value: 4

NCERT Computer Science Class 11 – Chapter 6 Solutions – Q 5

Posts created 29

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top