Program to Print Odd Numbers Within a Given Range

Python Program to Print Odd Numbers Within a Given Range

Given lower limit and upper limit, the task is to print all the odd numbers in the given range in Python.

Examples:

Example1:

Input:

given lower limit range = 25
given upper limit range = 79

Output:

25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79

Example2:

Input:

given lower limit range = 23
given upper limit range = 143

Output:

Odd numbers from 23 to 143 are :
23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95
97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143

Program to Print Odd Numbers Within a Given Range in Python

There are several ways to print odd numbers in the given range in Python some of them are:

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Method #1:Using for loop(Static Input)

Approach:

  • Give the lower limit and upper limit as static input and store them in two separate variables.
  • Loop from lower limit range to upper limit range using For loop.
  • Check if the iterator value is even or odd using the if statement and modulus operator.
  • If the iterator value is odd then print it.
  • The Exit of the program.

Below is the implementation:

# Give the lower limit and upper limit as static input
# and store them in two separate variables.
# given lower limit range
lowLimitRange = 23
# given upper limit range
uppLimitRange = 143
print('Odd numbers from', lowLimitRange, 'to', uppLimitRange, 'are :')
# Loop from lower limit range to upper limit range using For loop.
for iterval in range(lowLimitRange, uppLimitRange+1):
    # Check if the iterator value is even or odd using if statement and modulus operator.
    if(iterval % 2 != 0):
        # If the iterator value is odd then print it.
        print(iterval, end=" ")

Output:

Odd numbers from 23 to 143 are :
23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95
97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143

Method #2:Using for loop(User Input separated by spaces)

Approach:

  • Give the lower limit and upper limit as user input separated by spaces using map(), split() functions, and store them in two separate variables.
  • Loop from lower limit range to upper limit range using For loop.
  • Check if the iterator value is even or odd using the if statement and modulus operator.
  • If the iterator value is odd then print it.
  • The Exit of the program.

Below is the implementation:

# Give the lower limit and upper limit as user input separated by spaces using map(),split()
# functions and store them in two separate variables.
lowLimitRange, uppLimitRange = map(int, input(
    'Enter lower limit range and upper limit range separated by spaces = ').split())
print('Odd numbers from', lowLimitRange, 'to', uppLimitRange, 'are :')
# Loop from lower limit range to upper limit range using For loop.
for iterval in range(lowLimitRange, uppLimitRange+1):
    # Check if the iterator value is even or odd using if statement and modulus operator.
    if(iterval % 2 != 0):
        # If the iterator value is odd then print it.
        print(iterval, end=" ")

Output:

Enter lower limit range and upper limit range separated by spaces = 74 446
Odd numbers from 74 to 446 are :
75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133
 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 
187 189 191 193 195 197 199 201 203 205 207 209 211 213 215 217 219 221 223 225 227 229 231 233 235 237 
239 241 243 245 247 249 251 253 255 257 259 261 263 265 267 269 271 273 275 277 279 281 283 285 287 289 
291 293 295 297 299 301 303 305 307 309 311 313 315 317 319 321 323 325 327 329 331 333 335 337 339 341 
343 345 347 349 351 353 355 357 359 361 363 365 367 369 371 373 375 377 379 381 383 385 387 389 391 393
 395 397 399 401 403 405 407 409 411 413 415 417 419 421 423 425 427 429 431 433 435 437 439 441 443 445

Explanation:

  • The user must enter the upper and lower range limits.
  • The for loop spans from the lower to the top range limit.
  • The expression within the if-statement determines whether or not the remainder generated when the integer is divided by two is one (using the % operator).
  • If the remainder is not equal to zero, the number is odd, and so it is printed.

Method #3:Using While Loop(Static Input)

Approach:

  • Give the lower limit and upper limit as static input and store them in two separate variables.
  • Take a temporary variable and initialize it to a lower limit range.
  • Loop till temporary variable is less than or equal to the upper limit range using while loop.
  • Check if the iterator value is even or odd using the if statement and modulus operator.
  • If the iterator value is odd then print it.
  • Increase the value of the temporary variable by 1.
  • The Exit of the Program

Below is the implementation:

# Give the lower limit and upper limit as static input
# and store them in two separate variables.
# given lower limit range
lowLimitRange = 38
# given upper limit range
uppLimitRange = 281
# Take a temporary variable and initialize it to a lower limit range.
tempvar = lowLimitRange
print('Odd numbers from', lowLimitRange, 'to', uppLimitRange, 'are :')
# Loop till temporary variable is less than
# or equal to the upper limit range using while loop.
while(tempvar <= uppLimitRange):
    # Check if the iterator value is even or odd using if statement and modulus operator.
    if(tempvar % 2 != 0):
        # If the iterator value is odd then print it.
        print(tempvar, end=" ")
    # Increase the value of the temporary variable by 1.
    tempvar = tempvar+1

Output:

Odd numbers from 38 to 281 are :
39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 
109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 
161 163 165 167 169 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199 201 203 205 207 209 211
 213 215 217 219 221 223 225 227 229 231 233 235 237 239 241 243 245 247 249 251 253 255 257 259 261 263 
265 267 269 271 273 275 277 279 281

Method #4:Using While Loop(User Input)

Approach:

  • Give the lower limit and upper limit as user input separated by spaces using map(), split() functions, and store them in two separate variables.
  • Take a temporary variable and initialize it to a lower limit range.
  • Loop till temporary variable is less than or equal to the upper limit range using while loop.
  • Check if the iterator value is even or odd using the if statement and modulus operator.
  • If the iterator value is odd then print it.
  • Increase the value of the temporary variable by 1.
  • The Exit of the Program

Below is the implementation:

# Give the lower limit and upper limit as user input separated by spaces using map(),split()
# functions and store them in two separate variables.
lowLimitRange, uppLimitRange = map(int, input(
    'Enter lower limit range and upper limit range separated by spaces = ').split())
# Take a temporary variable and initialize it to a lower limit range.
tempvar = lowLimitRange
print('Odd numbers from', lowLimitRange, 'to', uppLimitRange, 'are :')
# Loop till temporary variable is less than
# or equal to the upper limit range using while loop.
while(tempvar <= uppLimitRange):
    # Check if the iterator value is even or odd using if statement and modulus operator.
    if(tempvar % 2 != 0):
        # If the iterator value is odd then print it.
        print(tempvar, end=" ")
    # Increase the value of the temporary variable by 1.
    tempvar = tempvar+1

Output:

Enter lower limit range and upper limit range separated by spaces = 325 785
Odd numbers from 325 to 785 are :
325 327 329 331 333 335 337 339 341 343 345 347 349 351 353 355 357 359 361 363 365 367 369 371 373 375 
377 379 381 383 385 387 389 391 393 395 397 399 401 403 405 407 409 411 413 415 417 419 421 423 425 427
 429 431 433 435 437 439 441 443 445 447 449 451 453 455 457 459 461 463 465 467 469 471 473 475 477 479
 481 483 485 487 489 491 493 495 497 499 501 503 505 507 509 511 513 515 517 519 521 523 525 527 529 531
 533 535 537 539 541 543 545 547 549 551 553 555 557 559 561 563 565 567 569 571 573 575 577 579 581 583 
585 587 589 591 593 595 597 599 601 603 605 607 609 611 613 615 617 619 621 623 625 627 629 631 633 635 
637 639 641 643 645 647 649 651 653 655 657 659 661 663 665 667 669 671 673 675 677 679 681 683 685 687 
689 691 693 695 697 699 701 703 705 707 709 711 713 715 717 719 721 723 725 727 729 731 733 735 737 739 
741 743 745 747 749 751 753 755 757 759 761 763 765 767 769 771 773 775 777 779 781 783 785

Method #5:Using List Comprehension

Approach:

  • Give the lower limit and upper limit as user input separated by spaces using map(), split() functions, and store them in two separate variables.
  • Using List Comprehension, for loop and if statements add all the odd numbers in the given range into the list.
  • Print the list.
  • The Exit of the Program

Below is the implementation:

# Give the lower limit and upper limit as user input separated by spaces using map(),split()
# functions and store them in two separate variables.
lowLimitRange, uppLimitRange = map(int, input(
    'Enter lower limit range and upper limit range separated by spaces = ').split())
# Using List Comprehension, for loop and if statements
# add all the odd numbers in the given range into the list.
oddnumList = [valu for valu in range(
    lowLimitRange, uppLimitRange+1) if valu % 2 != 0]
print('Odd numbers from', lowLimitRange, 'to', uppLimitRange, 'are :')
# Print the list.
for el in oddnumList:
    print(el, end=' ')

Output:

Enter lower limit range and upper limit range separated by spaces = 25 79
Odd numbers from 25 to 79 are :
25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79

Related Programs: