QuizCure
Find Available Solution Here!

Multiples of a Number in python

Deepak Oct 11, 2024

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 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 the loop index by num and print to get the 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 a 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 the number is a multiple of 3
if (isMultiple(10, 3) == True ):
    print("10 is a multiple of 3");
else:
    print("10 is not a multiple of 3");


# Check if the number is a multiple of 5
if (isMultiple(15, 5) == True ):
    print("15 is a multiple of 5");
else:
    print("15 is not multiple of ");
   
Result:
10 is not a multiple of 3
15 is a 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 demonstrating how to find multiples of 3 in the 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.

How to assign multiple values to one variable in Python?

In Python, it's not directly allowed to assign multiple values to a single variable but there is a workaround to achieve this please find below some effective ways:

1. Tuples:

  • Create an immutable list of values.
  • Useful for a fixed number of values.
values = (5, 10, 20)  # Assign multiple values (5,10,20) to a tuple
a, b, c = values  # extract values into separate corresponding variables
print(a, b, c)  # Result: (5, 10, 20)

2. Lists:

  • Create a mutable ordered collection of items.
  • Add/modify/remove items from the list
employee = ["Peter", "Maxim", "Chris"]  # Assign multiple items to a list
first_emp, second_emp, third_emp = employee  # Extract items
print(first_emp, second_emp, third_emp)  # Result ('Peter', 'Maxim', 'Chris')

3. Dictionaries:

  • Used to Store data values in key-value pairs, where keys are unique and values can be of any type of object in Python such as an integer, string, or list
  • Ideal for associating meaningful names as keys with values.
user = {"name": "Steffen", "age": 40, "country": "Germany"}  # Prepare key-value pairs
userName = user["country"]  # Access a value by its key 
print(userName)  # Result: Germany

4. Classes (for complex scenarios):

  • Creation of custom data types through class definitions with attributes and methods.
  • Best usage for modeling real-world objects and their relationships.
class Coordinates:
    def __init__(self, a, b):
        self.a = a
        self.b = b
  
coordinate = Coordinates(10, 20)  # Object creation with multiple attributes
print(coordinate.a, coordinate.b)  # Result: 10 20

Important points:

  • Choose the correct data structure based on your use cases:
    1) Use tuples for a fixed number of values.
    2) Use lists for Add/modify/remove items from the list
    3) Use Dictionaries for key-value pair associations
    4) Use Classes for Creation of custom data types through class definitions
  • Don’t forget to unpack values correctly while using them as individual variables.

Was this post helpful?

Send Feedback

Connect With QuizCure


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

Contributed By

Deepak

Deepak

QC STAFF
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Scroll up