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

 Live Demo

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.

Updated on: 16-Apr-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements