In most of the programming language, the program is executed sequentially i.e. the first statement is executed first, the second statement next and so on. But in some cases, we need to execute the same sequence of code time and again. For this purpose programming language provide certain control structures that allow more complicated code structure.
A loop statement allows us to execute a particular piece of code multiple times. Below is the diagram of a loop.
Python has 3 types of loops
- For loop
- While loop
- Nested loop
For more information on loops visit here.
(Diagram: Flow chart of a loop statement)
In this challenge, we’re going to use loops to help us do some simple math. Check out the Tutorial tab to learn more.
Given an integer, n, print its first 10 multiples. Each multiple n*I (where 1<=i<=10 ) should be printed on a new line in the form: n x i = result.
A single integer, n.
- 2<=n <=20
Print 10 lines of output; each line i (where 1<=i<=10 ) contains the result of n*i in the form:
n x i = result.
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
Hacker Rank 30 Days of Code Challenge
n = int(input().strip()) for i in range(1,11): print(n,'x',i,'=',n*i)
In the above solution we have applied a simple for loop. The second line iterate the value of i from 1 to 10 (Python ignore upper bound and include lower bound), and for each value of i print function prints the table.