Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

==========Iteration Statement=========

----> for loop

[syntax]:

for variable in range (start,end,updation):


-----------
-----------
-----------
-----------

[example]:
#Forward looping might not require the 3rd parameter for incrementing the
values seperately
for i in range (1,101,+1("Optional in forward")):
print(i,"Hello World")

#Backword looping must require the 3rd parameter for incrementing the values
seperately
for i in range (100,0,-1):
print(i,"Hello World")

[Question]:
WAP to Print table of any number

[Solution]:
num = int(input("Enter the number: "))
for i in range(1,11)
print(num,"*",i,"=",num*i)

[Interesting!]:
change in the value of variable inside a for loop that is being
used for incrementing the
loop forward, will not effect default sequence the loop
initialized earlier.

[HW Question]:
1: WAP to sum 1 to 100.
2: WAP to sum up odd and even numbers from 1 to 50000.
3: WAP to count and print occurence of multiple of 19 in range 1
to 5000.
4: WAP to Check whether a number a perfect number or not.
5: WAP to check whether a number is prime or not.

[HW Solution]:
3:
c=0
for i in range(19,5000):
if (i%19==0)
print(i,end ="\t")
c=c+100
print("\nTotal Number = ",c)

5:
num=int(input("Enter the Number: "))
for i in range(2,num):
if(num%i==0):
break
if(i==num and num>=2):
print("Prime")
else:
print("Not Prime")

[[IMPROVEMENTS]]

num=int(input("Enter the Number: "))


f=0
for i in range(2,num//2+1):
if(num%i==0):
f=1
break
if(i==num and num>=2):
print("Prime")
else:
print("Not Prime")

You might also like