Sunday, 12 January 2025

Day 90: Python Program to Find All Odd Palindrome Numbers in a Range without using Recursion

 


def is_palindrome(n):

    s = str(n)

    return s == s[::-1]

def find_odd_palindromes(start, end):

    odd_palindromes = []

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

        if num % 2 != 0 and is_palindrome(num):

            odd_palindromes.append(num)

    return odd_palindromes

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

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

result = find_odd_palindromes(start, end)

print(f"Odd palindrome numbers between {start} and {end}: {result}")

#source code --> clcoding.com 

1. Function Definition: is_palindrome(n)

def is_palindrome(n):

    s = str(n)

    return s == s[::-1]

Purpose: This function checks whether a number n is a palindrome.

Explanation:

s = str(n): Converts the number n to a string to easily reverse and compare its characters.

s[::-1]: This is a slicing operation that reverses the string s.

return s == s[::-1]: Compares the original string with the reversed string. If they are the same, the number is a palindrome (e.g., 121 is the same as 121 when reversed).

2. Function Definition: find_odd_palindromes(start, end)

def find_odd_palindromes(start, end):

    odd_palindromes = []

Purpose: This function finds all odd palindrome numbers within a given range, from start to end.

Explanation:

odd_palindromes = []: Initializes an empty list that will store the odd palindrome numbers found in the range.

3. Loop through the Range of Numbers

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

Purpose: This for loop iterates over all numbers in the range from start to end (inclusive).

Explanation:

range(start, end + 1): The range function generates numbers from start to end. end + 1 ensures that end is included in the range.

4. Check if the Number is Odd and a Palindrome

        if num % 2 != 0 and is_palindrome(num):

            odd_palindromes.append(num)

Purpose: Checks if the number num is both odd and a palindrome.

Explanation:

num % 2 != 0: This condition checks if the number num is odd. If the remainder when divided by 2 is not zero, it's odd.

is_palindrome(num): This calls the is_palindrome function to check if the number is a palindrome.

If both conditions are true, the number num is added to the list odd_palindromes.

5. Return the List of Odd Palindromes

    return odd_palindromes

Purpose: After the loop finishes, the function returns the list of odd palindrome numbers that were found.

6. Get User Input for Range

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

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

Purpose: This takes input from the user to define the range for finding odd palindromes.

Explanation:

input("Enter the start of the range: "): Prompts the user to enter the starting value of the range.

input("Enter the end of the range: "): Prompts the user to enter the ending value of the range.

Both inputs are converted to integers using int() because input() returns data as a string.

7. Find Odd Palindromes and Display the Result

result = find_odd_palindromes(start, end)

print(f"Odd palindrome numbers between {start} and {end}: {result}")

Purpose: This calls the find_odd_palindromes function and prints the result.

Explanation:

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

print(f"Odd palindrome numbers between {start} and {end}: {result}"): Prints the result in a formatted string showing the list of odd palindromes found in the range.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (87) AI (35) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (179) C (77) C# (12) C++ (83) Course (67) Coursera (231) Cybersecurity (24) Data Analytics (1) data management (11) Data Science (132) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Euron (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (4) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (62) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (958) Python Coding Challenge (398) Python Quiz (54) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

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