Multiples of a Number in python
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.
Scroll for More Useful Information and Relevant FAQs
- Print multiples of a given number using while loop
- Find multiples of a number using range function and for loop
- Check if the number is multiple of m in python
- Find multiple of any given number in the list & range of numbers
- Feedback: Your input is valuable to us. Please provide feedback on this article.
Print multiples of a given number using while loop
For demonstration purposes, we will see the following program to print multiples of 5 in Python using a while loop
def getMultiples(num, number_of_multiples):
i = 1
while i <= number_of_multiples:
print(num*i)
i += 1
getMultiples(5, 5);
Result:
5
10
15
20
25
Explanation:
- Created function getMultiples function to generate number_of_multiples for given number num.
-
Running while loop until indexing variable it meets condition
i <= number_of_multiples
- Call getMultiples to get a list of multiples of 5.
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.
Brain Exercise
