{"id":10359,"date":"2021-10-01T10:30:16","date_gmt":"2021-10-01T05:00:16","guid":{"rendered":"https:\/\/python-programs.com\/?p=10359"},"modified":"2021-11-22T18:33:28","modified_gmt":"2021-11-22T13:03:28","slug":"python-program-to-print-odd-numbers-within-a-given-range","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-odd-numbers-within-a-given-range\/","title":{"rendered":"Python Program to Print Odd Numbers Within a Given Range"},"content":{"rendered":"
Given lower limit and upper limit, the task is to print all the odd numbers in the given range in Python.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to print odd numbers in the given range in Python some of them are:<\/p>\n Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples<\/a> and try to code all of them on your own then check with the exact code provided by expert programmers.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\ngiven lower limit range = 25\r\ngiven upper limit range = 79<\/pre>\n
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<\/pre>\n
given lower limit range = 23\r\ngiven upper limit range = 143<\/pre>\n
Odd numbers from 23 to 143 are :\r\n23 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\r\n97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143<\/pre>\n
Program to Print Odd Numbers Within a Given Range in Python<\/h2>\n
\n
Method #1:Using for loop(Static Input)<\/h3>\n
\n
# Give the lower limit and upper limit as static input\r\n# and store them in two separate variables.\r\n# given lower limit range\r\nlowLimitRange = 23\r\n# given upper limit range\r\nuppLimitRange = 143\r\nprint('Odd numbers from', lowLimitRange, 'to', uppLimitRange, 'are :')\r\n# Loop from lower limit range to upper limit range using For loop.\r\nfor iterval in range(lowLimitRange, uppLimitRange+1):\r\n # Check if the iterator value is even or odd using if statement and modulus operator.\r\n if(iterval % 2 != 0):\r\n # If the iterator value is odd then print it.\r\n print(iterval, end=\" \")\r\n<\/pre>\n
Odd numbers from 23 to 143 are :\r\n23 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\r\n97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143<\/pre>\n
Method #2:Using for loop(User Input separated by spaces)<\/h3>\n
\n
# Give the lower limit and upper limit as user input separated by spaces using map(),split()\r\n# functions and store them in two separate variables.\r\nlowLimitRange, uppLimitRange = map(int, input(\r\n 'Enter lower limit range and upper limit range separated by spaces = ').split())\r\nprint('Odd numbers from', lowLimitRange, 'to', uppLimitRange, 'are :')\r\n# Loop from lower limit range to upper limit range using For loop.\r\nfor iterval in range(lowLimitRange, uppLimitRange+1):\r\n # Check if the iterator value is even or odd using if statement and modulus operator.\r\n if(iterval % 2 != 0):\r\n # If the iterator value is odd then print it.\r\n print(iterval, end=\" \")\r\n<\/pre>\n
Enter lower limit range and upper limit range separated by spaces = 74 446\r\nOdd numbers from 74 to 446 are :\r\n75 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\r\n 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 \r\n187 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 \r\n239 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 \r\n291 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 \r\n343 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\r\n 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<\/pre>\n
\n
Method #3:Using While Loop(Static Input)<\/h3>\n
\n
# Give the lower limit and upper limit as static input\r\n# and store them in two separate variables.\r\n# given lower limit range\r\nlowLimitRange = 38\r\n# given upper limit range\r\nuppLimitRange = 281\r\n# Take a temporary variable and initialize it to a lower limit range.\r\ntempvar = lowLimitRange\r\nprint('Odd numbers from', lowLimitRange, 'to', uppLimitRange, 'are :')\r\n# Loop till temporary variable is less than\r\n# or equal to the upper limit range using while loop.\r\nwhile(tempvar <= uppLimitRange):\r\n # Check if the iterator value is even or odd using if statement and modulus operator.\r\n if(tempvar % 2 != 0):\r\n # If the iterator value is odd then print it.\r\n print(tempvar, end=\" \")\r\n # Increase the value of the temporary variable by 1.\r\n tempvar = tempvar+1\r\n<\/pre>\n
Odd numbers from 38 to 281 are :\r\n39 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 \r\n109 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 \r\n161 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\r\n 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 \r\n265 267 269 271 273 275 277 279 281<\/pre>\n
Method #4:Using While Loop(User Input)<\/h3>\n
\n
# Give the lower limit and upper limit as user input separated by spaces using map(),split()\r\n# functions and store them in two separate variables.\r\nlowLimitRange, uppLimitRange = map(int, input(\r\n 'Enter lower limit range and upper limit range separated by spaces = ').split())\r\n# Take a temporary variable and initialize it to a lower limit range.\r\ntempvar = lowLimitRange\r\nprint('Odd numbers from', lowLimitRange, 'to', uppLimitRange, 'are :')\r\n# Loop till temporary variable is less than\r\n# or equal to the upper limit range using while loop.\r\nwhile(tempvar <= uppLimitRange):\r\n # Check if the iterator value is even or odd using if statement and modulus operator.\r\n if(tempvar % 2 != 0):\r\n # If the iterator value is odd then print it.\r\n print(tempvar, end=\" \")\r\n # Increase the value of the temporary variable by 1.\r\n tempvar = tempvar+1\r\n<\/pre>\n
Enter lower limit range and upper limit range separated by spaces = 325 785\r\nOdd numbers from 325 to 785 are :\r\n325 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 \r\n377 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\r\n 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\r\n 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\r\n 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 \r\n585 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 \r\n637 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 \r\n689 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 \r\n741 743 745 747 749 751 753 755 757 759 761 763 765 767 769 771 773 775 777 779 781 783 785<\/pre>\n
Method #5:Using List Comprehension<\/h3>\n
\n
# Give the lower limit and upper limit as user input separated by spaces using map(),split()\r\n# functions and store them in two separate variables.\r\nlowLimitRange, uppLimitRange = map(int, input(\r\n 'Enter lower limit range and upper limit range separated by spaces = ').split())\r\n# Using List Comprehension, for loop and if statements\r\n# add all the odd numbers in the given range into the list.\r\noddnumList = [valu for valu in range(\r\n lowLimitRange, uppLimitRange+1) if valu % 2 != 0]\r\nprint('Odd numbers from', lowLimitRange, 'to', uppLimitRange, 'are :')\r\n# Print the list.\r\nfor el in oddnumList:\r\n print(el, end=' ')\r\n<\/pre>\n
Enter lower limit range and upper limit range separated by spaces = 25 79\r\nOdd numbers from 25 to 79 are :\r\n25 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<\/pre>\n
\n