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