QuizCure
Find Available Solution Here!

Multiples of a Number in python

QuizCure Member Mar 19, 2023

Multiple of any given number can be determined by following basic rules.

Suppose we need to extract a multiple of a given number x and require to find n multiples for x.

Here is a list of n multiples for a given number x

X*1, X*2, x*3 .. X*n

Example: 

X = 6
N = 5

Multiples : 6, 12, 18, 24, 30

We will strongly recommend you to first try to write a program to generate multiples as explained above.

Now we will explore how to generate multiples of a number in python. Let's start to learn in the following step-by-step manner.

Find multiples of a number using range function and for loop

Let's understand with the following example of printing multiples of 3 using the range.

def getMultiples(num, number_of_multiples):
	for i in range(1, (number_of_multiples+1)):
		print(num*i)


getMultiples(3, 5);
Result:
3
6
9
12
15
   

Explanation:

  • Here the range function is constructed with the start value as 1 (inclusive) and stop value as number_of_multiples+1 (exclusive) so that the for loop can iterate from 1 to number_of_multiples times.
  • Executing a for loop over a range of specified values
  • Multiply loop index by num and print to get desired multiples as shown above.

Check if the number is multiple of m in python

To ensure whether a given number is multiple of m we need to check if that number is divisible by m or not.

So for this purpose, we will check the use of the Python modulo operator (%) to calculate the remainder of a division. We can mark m as multiple of a given number If the remainder is zero.

Below example to check if a given number is a multiple of 5 in Python

def isMultiple(num,  check_with):
	return num % check_with == 0;
	

# Check if number is multiple of 3
if (isMultiple(10, 3) == True ):
    print("10 is multiple of 3");
else:
    print("10 is not multiple of 3");


# Check if number is multiple of 5
if (isMultiple(15, 5) == True ):
    print("15 is multiple of 5");
else:
    print("15 is not multiple of ");
   
Result:
10 is not multiple of 3
15 is multiple of 5
    

Here is what the above code means:

Condition num % check_with == 0 return True if num is divisible by check_with that means remainder is zero otherwise will return False.

Find multiple of any given number in the list & range of numbers

Here is the code demonstrates to find multiples of 3 in list and range as below:

def isMultiple(num,  check_with):
	return num % check_with == 0;
	

print ("Multiples of 3 in the range are :")
for i in range(1, 10):
	if (isMultiple(i, 3) == True ):
		print(i);		


print ("Multiples of 3 in the  list are :")
nums = [50, 11, 32, 23, 18, 91, 90, 12]

for j in nums:
	if (isMultiple(j, 3) == True ):
		print(j);	
    
Result:
Multiples of 3 in the range are :
3
6
9
Multiples of 3 in the list are :
18
90
12
   

Code Explanation:

  • Created function isMultiple as described in the previous example.
  • Loop through a range of numbers and pass a range of numbers to isMultiple to check if divisible by 3 or not. Printed if divisible.
  • Loop through list nums and check if divisible by 3 bypassing each list element to isMultiplefunction and print that meets the condition.

Was this post helpful?

Send Feedback

Connect With QuizCure


Follow Us and Stay tuned with upcoming blog posts and updates.

Contributed By

QuizCure Member
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Other Python Articles