Skip to main content

GRADE XI - FIND THE OUTPUT : LOOPS

 

                                                PRACTICE QUESTIONS

                                              FIND THE OUTPUT : LOOPS


#FIND THE OUTPUT

#1.

i=0

sum=0

while i<9:

    if i%4==0:

        sum=sum+i

        print("inside loop",sum)

        print("inside loop value of i",i)

    i=i+2

print(sum)

print('**'*40) 


#2.

x=10

y=0

while x>y:

    x=x-4

    y+=4

print(x,end=' ')

print('**'*40) 


#3.

p,q=0,2

p=p+q 

q=p+4 

print(p)

print(q)


#4.

x=10

y=0

while x>y:

    print(x,y)

    x-=1

    y+=1


#5.

i=1

while i<=7:

    print("i=",i)

    i+=1

print("Final i",i)


#6.

num=30

while num>0:

    print(num%10)

    num=num//10


#7.

i=0

while i<10:

    i=i+1

    if i==5:

        break

    else:

        print(i,end=' ')

    print(i,end=' ')


#8.

i=1

while i<7:

    print("i=",i)

    i+=1

    if i==5

    break

#9

n=-100

while n<100:

    print(n,end=' ')

    n=n+100


#10.

x=2

y=z*2-x

y=3

s=0

while s<z:

    print(s)

    s=s+1


11. What will be the output:

t, s = 0,1

for i in range(10,1,-3):

        if i% 4 ==0:

                t -= i

                break

else:

                s += i

print(s,t)


12. Rewrite the equivalent code using while loop

for i in range(-5,1,1):

        print(i)

Answer : 

i = -5

while i < 1:

        print(i)

        i += 1

13. What will be the output:
for i in range(1,10,2):
    if i% 7 == 0:
continue
    elif  i% 5 ==0:
    i+=5
print(i,end= " ")

x---------------------------------------------------------x-------------------------------------------------x
 


Comments

Popular posts from this blog

CS - SORTING/SEARCHING ALGORITHMS

  SORTING ALGORITHMS                       SORTING ALGORITHM PDF LINK #Bubble Sort          ·        The outer loop iterates through the entire array. ·        The inner loop compares adjacent elements and swaps them if they are out of order. ·        The outer loop runs n times, and each pass moves the largest element to its correct position. arr=[3,8,5,2,1] n = len(arr) print(n) for i in range(n):  #traverse through all the elements         # Last i elements are already sorted, no need to check them         for j in range(0, n-i-1):              # Swap if the element found is greater than the next element              if arr[j] > arr[j+1]:               ...

GRADE XI - NESTED FOR LOOP

                                                         NESTED FOR LOOP 1. for var1 in range(3):      print(var1,"OUTER LOOP")          # 0 outer loop       1 outer loop      2 outer loop          for var2 in range(2):                  print(var2+1,"INNER LOOP")    #1  2 inner loop     1  2  inner loop   1 2 inner loop  2. Print the following pattern using for loop: 1  1 2  1 2 3  1 2 3 4  Sol: for r in range(1,5):   #ROWS     for c in range(1,r+1):   #COLUMNS         print(c,end=" ")     print() 3. Print the following pattern using for loop: @  @ @  @ @ @...