DEV Community

varatharajan
varatharajan

Posted on

looping

while :

Task :

1) 1 2 3 4 5 6 7 8 9 10
2) 10 9 8 7 6 5 4 3 2 1
3) 1 3 5 7 9
4) 2 4 6 8 10
5) 3 6 9

input :

#1 2 3 4 5 6 7 8 9 10
no = 1
while no<=10:
    print(no, end=' ')
    no+=1

print()

# 10 9 8 7 6 5 4 3 2 1
no = 10
while no>=1:
    print(no, end=' ')
    no-=1

print()

# 1 3 5 7 9 
no = 1
while no<=10:
    print(no, end=' ')
    no+=2

print()

#2 4 6 8 10
no=2
while no<=10:
    print(no, end=' ')
    no+=2

print()

# 3 6 9
no = 3
while no<=10:
    print(no, end=' ')
    no+=3
Enter fullscreen mode Exit fullscreen mode

output :

1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
1 3 5 7 9
2 4 6 8 10
3 6 9

using '*'
input

# 3 6 9 12 15 18 21
no = 1
while no<=7:
    print(no*3, end=' ')
    no+=1
Enter fullscreen mode Exit fullscreen mode

output

3 6 9 12 15 18 21

Task :
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15

input

no = 1 
while no<=10:
    result = no*5
    print(f"{no}*5 = {result}")
    no+=1
Enter fullscreen mode Exit fullscreen mode

output

1*5 = 5
2*5 = 10
3*5 = 15
4*5 = 20
5*5 = 25
6*5 = 30
7*5 = 35
8*5 = 40
9*5 = 45
10*5 = 50

example :

1*2=2
3*2=6
5*2=10
7*2=14
9*2=18

input

no = 1
while no<=10:
    result=no*2
    print(f"{no}*2={result}")
    no+=2
Enter fullscreen mode Exit fullscreen mode

output

1*2=2
3*2=6
5*2=10
7*2=14
9*2=18

example :

#1 3 5 7 9 2 4 6 8 10

no = 1
while no<=10:
    print(no, end = ' ')
    if no==9:
       no=0
    no+=2
Enter fullscreen mode Exit fullscreen mode

output

1 3 5 7 9 2 4 6 8 10

Top comments (0)