Finding the multiples of a number in a given list using NumPy

PythonNumpyServer Side ProgrammingProgramming

In this program, we will find the index position at which a multiple of a given number exists. We will use both the Numpy and the Pandas library for this task.

Algorithm

Step 1: Define a Pandas series.
Step 2: Input a number n from the user.
Step 3: Find the multiples of that number from the series using argwhere() function in the numpy library.

Example Code

import numpy as np

listnum = np.arange(1,20)
multiples = []

print("NumList:\n",listnum)
n = int(input("Enter the number you want to find multiples of: "))
for num in listnum:
   if num % n == 0:
      multiples.append(num)
print("Multiples of {} are {}".format(n, multiples))

Output

NumList:
[1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
Enter the number you want to find multiples of: 5
Multiples of 5 are [5, 10, 15]
raja
Published on 16-Mar-2021 10:53:16
Advertisements