Saturday, 7 December 2024

Day 18 : Python Program to Find Numbers which are Divisible by 7 and multiple of 5 in a Given Range


 def find_numbers(start, end):

    print(f"Numbers divisible by 7 and multiple of 5 between {start} and {end}:")

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

        if num % 7 == 0 and num % 5 == 0: 

            print(num, end=' ')

    print() 

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

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

find_numbers(start, end)


Code Explanation:

1. Function Definition

def find_numbers(start, end):

def: Declares a new function.

find_numbers: The name of the function, which finds numbers meeting specific criteria within a range.

start, end: Input parameters representing the range boundaries.

2. Displaying the Purpose

    print(f"Numbers divisible by 7 and multiple of 5 between {start} and {end}:")

print(): Displays a message to indicate what the program is doing.

f-string: Used to format the message dynamically, inserting the start and end values into the string.

3. Iterating Through the Range

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

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

A for loop that iterates over every number (num) in the range from start to end (inclusive).

range(start, end + 1): Generates a sequence of numbers from start to end.

4. Checking Conditions

        if num % 7 == 0 and num % 5 == 0:

if: Conditional statement to check if num meets both criteria:

num % 7 == 0: Checks if num is divisible by 7 (remainder is 0).

num % 5 == 0: Checks if num is divisible by 5 (remainder is 0).

and: Combines both conditions; both must be true for the if block to execute.

5. Printing the Numbers

            print(num, end=' ')

print(num, end=' '):

Prints num if it meets the conditions.

end=' ': Keeps the output on the same line with a space between numbers instead of starting a new line.

6. Adding a New Line

    print()

print(): Prints an empty line after the loop to ensure a clean output.

7. Taking Input for the Range

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

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

input("..."): Prompts the user to enter the start and end values for the range.

int(): Converts the user input (string) into an integer.

8. Calling the Function

find_numbers(start, end)

find_numbers(start, end): Calls the function with the user-provided start and end values.

What the Program Does

It identifies all numbers between start and end (inclusive) that are:

Divisible by 7.

Multiples of 5 (or equivalently divisible by 5).


#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