HackerRank Day 5 Solution in Python: Loops

Today we will see the HackerRank Day 5 Solution in Python. The problem is named Loops which is part of 30 Days of code on HackerRank. Let’s get started!

Day 5: Loops Problem statement

We are given an integer n, our task is to print its first 10 multiples. Each multiple n x i  should be printed on a new line in the form: n x i = result.

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

Explanation: Prints 10 lines of output; in which each line contains the result of 2 x i in the form, 2 x i = result.

You can solve the problem here.

HackerRank Day 5 Solution in Python

#!/bin/python3

import math
import os
import random
import re
import sys

if __name__ == '__main__':
    n = int(input())
    count=1
    #Prints multiples of the given number upto 10
    while count<=10:
        print(n,"x",count,"=",count*n)
        #Increment the count variable
        count+=1

Code Explanation

  • First we get the input n from the user
  • Then we initialise a count variable with 1
  • Then in a loop we start printing the values with the given condition. That is the multiples of the given number upto 10
  • Increment the count variable after printing each multiple

Also Read:

Share:

Author: Ayush Purawr