Monday, 9 December 2024

Day 23 : Python Program to Print All Odd Numbers in a given Range


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

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

print("Odd numbers between", start, "and", end, "are:")

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

    if num % 2 != 0: 

        print(num, end=" ")  


Code Explanation:

1. Prompt User for Input:

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

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

The program prompts the user to input two integers:

start: The beginning of the range.

end: The end of the range.

int(input(...)) ensures the input is converted from a string to an integer.

2. Print a Header:

print("Odd numbers between", start, "and", end, "are:")

This line prints a message specifying the range of numbers being analyzed.

3. Loop Through the Range:

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

range(start, end + 1) generates all integers from start to end (inclusive).

The loop iterates through each number in this range, assigning it to the variable num.

4. Check for Odd Numbers:

if num % 2 != 0:

This checks whether num is odd:

num % 2: Computes the remainder when num is divided by 2.

!= 0: Ensures the remainder is not zero (odd numbers always leave a remainder of 1).

5. Print Odd Numbers:

print(num, end=" ")

If num is odd, it is printed on the same line.

end=" " ensures the numbers are printed with a space in between, instead of a new line after each.


#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