A list Divisibility contains the following elements:

9, 35, 4, 19, 55, 12, 55, 36

Write a program to swap the content with next value divisible by 5 so that the resultant list will look like

9,4, 35, 19, 12, 55, 36, 55

 

Answer:

Code:

Divisibility = [ 9 , 35 , 4 , 19 , 55 , 12 , 55 , 36

n = len(Divisibility) 

i = 0  

 

while i<n: 

    if Divisibility[i]% 5 == 0

        temp=Divisibility[i] 

        Divisibility[i]=Divisibility[i+ 1

        Divisibility[i+ 1 ]=temp

        i = i + 2  

    else: 

i = i + 1  

     

print(Divisibility)

 

Question 4 Long Answer.jpg

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.