- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Print all Integers that are not Divisible by Either 2 or 3 and Lie between 1 and 50
When it is required to print all the elements which can’t be divided by 2 or 3 and lie between 1 and 50, the constraints are mentioned in the form of a ‘while’ loop and ‘if’ condition.
Below is a demonstration of the same −
Example
print("Integers not divisible by 2 and 3, that lie between 1 and 50 are : ") n = 1 while n <= 51: if n % 2 != 0 and n % 3 != 0: print(n) n = n+1
Output
Integers not divisible by 2 and 3, that lie between 1 and 50 are : 1 5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49
Explanation
The value of n is assigned to 1.
A while loop runs until this ‘n’ is not greater than 51,
It checks to see if the number is divisible by 2 or 3 or not.
If it is not divisible, the number is displayed on the console.
After every display, it is incremented.
Advertisements