Home » Python programs

Python | Program to print all numbers which are divisible by M and N in the List



Here, we will learn how to print all numbers from the string which are divisible by M an N in Python?
Submitted by IncludeHelp, on July 27, 2018

Given a list of the integers and M, N and we have to print the numbers which are divisible by M, N in Python.

Example:

    Input:
    List = [10, 15, 20, 25, 30]
    M = 3, N=5

    Output:
    15
    30

To find and print the list of the numbers which are divisible by M and N, we have to traverse all elements using a loop, and check whether the number is divisible by M and N, if number is divisible by M and N both print the number.

Program:

# declare a list of integers
list = [10, 15, 20, 25, 30]

# declare and assign M and N 
M = 3
N = 5 

# print the list
print "List is: ", list

# Traverse each element and check 
# whether it is divisible by M, N 
# or not, if condition is true print 
# the element
print "Numbers divisible by {0} and {1}".format (M, N)
for num in list:
	if( num%M==0 and num%N==0 ) :
		print num

Output

    List is:  [10, 15, 20, 25, 30]
    Numbers divisible by 3 and 5
    15
    30





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Quick links
Latest articles, Internship, Members
New...
Coding problems, Algorithms, Discrete Mathematics, Big data
Languages
C, C++, C++ STL, Java, Data Structure, C#.Net, Android, Kotlin, SQL
Web
PHP, Python, JavaScript, CSS, Ajax, Node.js, Web prog.
Programs
C, C++, DS, Java, C#, Python
Aptitude
C, C++, Java, DBMS
Interview
C, Embedded C, Java, SEO, HR
CS Subjects
CS Basics O.S. Networks DBMS Embedded Systems Cloud Computing Machine learning CS Organizations Linux DOS
More...
Articles, Puzzles, News/Updates


Recommended posts
C Tips & Tricks, C++ Tips & Tricks
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distros of 2018
C programming optimization techniques
Differences b/w C & Embedded C?
Embedded C Interview Q. & A.
C programming tips for Embedded Development
Basic rules of writing a C program
Important points (rules) to remember while writing C/C++ program
Top 5 Websites for solving programming challenges
Read more...


Others...
Computer G.K. (MCQ)
Most viewed pages...
Categories...



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.