DEV Community

M__
M__

Posted on

DAY 5: LOOPS

A loop is a piece of code that is executed a number of times as long as a condition is true.
Python has two kinds of loops:

  1. For- loop (used to iterate a list or string or any object that is iterable)
  2. While-loop (executes a statement as long as a condition is true) The task for this challenge used ‘while’ loop given an integer and print its 10 multiples, just like a mathematical table.
#value whose multiplication table you would like
n = int(input())

#value from which multiplication begins
i = 1

#looping through i while it is in the range of 1 to 11
#11 is the stop value and will not be included in the multiplication
while i in range(1,11):

    #res variable hold the result of multiplication
    res = n * i
    print(f'{n} x {i} = {res}')

    #keep on increasing the value of i as you continue.
    i += 1
''' Sample Input: 2'''
'''Sample Output: 
   2 x 1 = 2
   2 x 2 = 4
   2 x 3 = 6
   2 x 4 = 8
   2 x 5 = 10
   2 x 6 = 12
   2 x 7 = 14
   2 x 8 = 16
   2 x 9 = 18
   2 x 10 = 20
'''
Enter fullscreen mode Exit fullscreen mode

One thing I have to say is that the solutions I post here are from my personal execution and I am aware that we all program differently and someone else may have a different technique they used to solve the challenge and that is highly encouraged and allowed🙂.

Top comments (0)