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]:               ...

PYTHON - MYSQL CONNECTIVITY CODE

  #INSERTION OF DATA import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="root", database="school" ) print("Successfully Connected") #print(mydb) mycursor=mydb.cursor()   v1=int(input("enter ID:")) v2=input("enter name:") v3=input("enter Gender:") v4=int(input("enter age:")) sql='insert into TEACH values("%d","%s","%s","%s")'%(v1,v2,v3,v4) print(sql) mycursor.execute(sql) mydb.commit() print("record added") #MYSQL Connection code – Deletion on database SOURCE CODE: s=int(input("enter id of TEACHER to be deleted:")) r=(s,) v="delete from TEACH where id=%s" mycursor.execute(v,r) mydb.commit() print("record deleted") MYSQL Connection code – Updation on database SOURCE CODE: import mysql.connector mydb = mysql.connector.c...