Python Program to Print All Integers that Aren’t Divisible by Either 2 or 3

This is a Python Program to print all integers that aren’t divisible by either 2 or 3 and lies between 1 and 50.

Problem Description

The program prints all integers that aren’t divisible by either 2 or 3 and lies between 1 and 50.

Problem Solution

1. Use a for-loop ranging from 0 to 51.
2. Then use an if statement to check if the number isn’t divisible by both 2 and 3.
3. Print the numbers satisfying the condition.
4. Exit.

Program/Source Code

Here is source code of the Python Program to print all integers that aren’t divisible by either 2 or 3 and lies between 1 and 50. The program output is also shown below.

 
for i in range(0,51):
    if(i%2!=0&i%3!=0):
        print(i)
Program Explanation

1. The for loop ranges from 0 to 50 (as 51 isn’t exclusive).
2. The expression within the if-statement checks if the remainder obtained when the number divided by 2 and 3 is one or not.
3. If the remainder isn’t equal to 0, the number isn’t divisible by either 2 and 3.
4. The number satisfying the condition is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
1
5
7
11
13
17
19
23
25
29
31
35
37
41
43
47
49

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Note: Join free Sanfoundry classes at Telegram or Youtube

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.