How do i print multiple of 5 in python?

In this programming article, we are going to learn

  • program to find multiples of a given number in python

Python program to print multiples of a given number

The program to find the multiple sof a number in python is as follows:

# Owner : TutorialsInhand Author : Devjeet Roy

number = int[input["Enter number: "]]

print["The multiples are: "]
for i in range[1,11]:
    print[number*i, end =" "]

The output of the program to print multiples of a given number in python is as follows:

PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter number: 5
The multiples are:
5 10 15 20 25 30 35 40 45 50

Few important tips about the program

1. In the program we take a user input and convert it to integer data type.

2. We have found out the first ten multiples of the number.

3. We have to run a loop from 1 to 10 and have multiplied it with the number to get its multiples.

Program to print multiples of a given number in python snapshot:

Given the above approach, try to print the first n multiples of a given number in python. Try to do it as hands-on.


Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author

Devjeet Roy
Full Stack Web Developer & Data Science Enthusiast

Page Views :    Published Date : Dec 30,2021  

General feedback

I will try to go over some points that can be useful to note. Firstly there are several things I like about your code. Firstly it is very readable. Secondly I like that you split your logic. You also split finding the string and printing it. This is good. With this being said there are always things which could and should be improved

Semantics

You should use the if __name__ == "__main__": module in your answer.

def fizz_buzz[num]:
    if num%3==0 and num%5==0:
        return 'FizzBuzz'

    elif num % 3 == 0:
        return 'Fizz'

    elif num % 5==0:
        return 'Buzz'
    else:
        return num

if __name__ == "__main__":

    for n in range[1,100]:
        print[fizz_buzz[n]]

Which makes your code reusable for later. Eg you can call functions from your file in other programs. Your else clause at the end of the code is useless. You could have written

    elif num % 5==0:
        return 'Buzz'
    return num

Alternatives

One problem with your code is that you have multiple exit points. Now this is not something to sweat too hard over, and it is not a goal to always try have a single exit. However it can be easier to debug a code with fewer exit points. This is of course much more relevant in longer and more complex code. Though it is a good thing to always have in mind. One way to do this is to define a new variable string

def fizz_buzz[num]:
    string = ''
    if num%3==0 and num%5==0:
        string = 'FizzBuzz'

    elif num % 3 == 0:
        string = 'Fizz'

    elif num % 5==0:
        string = 'Buzz'

    if string:
       return string
    return num

The code now only has two exit points however it can still be improved. One key point is that if a number is divisible by 3 and 5, it is divisible by 15. So we can gradually build the string, like shown below

def fizz_buzz[num]:
    string = ''

    if num % 3 == 0:
        string += 'Fizz'

    if num % 5==0:
        string += 'Buzz'

    if string:
       return string
    return num

As a last point the return statement could be written using a terniary conditional operator

return string if string else n

Which combines the two exit points into a single one. To sumarize

def fizz_buzz[num]:
    string = ''
    if num % 3==0: string +='Fizz' 
    if num % 5==0: string +='Buzz'
    return string if string else num

if __name__ == "__main__":

    for n in range[1, 100]:
        print[fizz_buzz[n]]

Closing comments

Python has a style guide PEP 8 which explains in excruciating detail how to structure your code. I whole heartily recommend skimming through it and follow it.

The problem FizzBuzz is very, very simple. It can be solved in a number of ways using just a simple line. Syb0rg, showed one way to write this code

for i in range[1,101]: print["Fizz"*[i%3==0] + "Buzz"*[i%5==0] or i]

You can even shorten this into

i=0;exec"print i%3/2*'Fizz'+i%5/4*'Buzz'or-~i;i+=1;"*100 

Using some cryptic pythonic voodoo. However as I said in the introductory I like your code, because it is easy to understand. Almost always it is better to have clear, readable code than cryptic code which is a few lines shorter. This of course disregards any speed improvements and such

How do you check for multiples of 5 in Python?

In Python, the remainder operator [“%”] is used to check the divisibility of a number with 5. If the number%5 == 0, then it will be divisible.

How do I print multiples of a number in Python?

We can use range[] function in Python to store the multiples in a range. First we store the numbers till m multiples using range[] function in an array, and then print the array with using [*a] which print the array without using loop.

How do I print multiple of 3 in Python?

Here's a Python program to do that, n = int[input['Enter any number: ']] print[list[range[0,n+1,3]]].
n = int[input[" Enter the value of n : "].
list1 = [ ].
for i in range[ 1 , n ]:.
if [ i % 3 == 0 ]:.
list1.append[i].
print [f" This are multiples of 3 : \n{list1}. "].

Chủ Đề