Sunday, 8 December 2024

Day 22 : Python Program to Print All Integers that are 'not Divisible by Either 2 or 3

 


def print_not_divisible_by_2_or_3(start, end):

    print(f"Integers between {start} and {end} not divisible by 2 or 3:")

    for num in range(start, end + 1):

          if num % 2 != 0 and num % 3 != 0:

            print(num, end=' ')

    print()  

start = int(input("Enter the start of the range: "))

end = int(input("Enter the end of the range: "))

print_not_divisible_by_2_or_3(start, end)


Code Explanation:

1. Function Definition:

def print_not_divisible_by_2_or_3(start, end):

This defines a function named print_not_divisible_by_2_or_3, which takes two arguments:
start: The starting number of the range.
end: The ending number of the range.

2. Print a Header:

print(f"Integers between {start} and {end} not divisible by 2 or 3:")
This line prints a message indicating the range of numbers that will be checked.

3. Loop Through the Range:

for num in range(start, end + 1):
This loop iterates through all integers from start to end (inclusive).
range(start, end + 1) generates the sequence of numbers within this range.

4. Check the Condition:

if num % 2 != 0 and num % 3 != 0:
num % 2 != 0: Checks if the number is not divisible by 2 (i.e., it's odd).
num % 3 != 0: Checks if the number is not divisible by 3.
The condition ensures the number meets both criteria (not divisible by either 2 or 3).

5. Print Numbers Meeting the Condition:

print(num, end=' ')
If the condition is met, the number is printed on the same line.
end=' ' ensures the numbers are printed with a space instead of a newline after each.

6. End of Function:

print()
After the loop completes, this adds a newline to separate the output from subsequent program output.

Main Program:
start = int(input("Enter the start of the range: "))
end = int(input("Enter the end of the range: "))
The user is prompted to input the starting (start) and ending (end) numbers of the range.
int(input(...)) ensures the input is converted from a string to an integer.

Call the Function:
print_not_divisible_by_2_or_3(start, end)
The function is called with start and end as arguments, performing the checks and printing the results.

#source code --> clcoding.com 

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (28) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (223) Cybersecurity (24) data management (11) Data Science (127) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (4) PHP (20) Projects (29) Python (923) Python Coding Challenge (318) Python Quiz (4) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses