{"id":9218,"date":"2021-09-30T17:30:10","date_gmt":"2021-09-30T12:00:10","guid":{"rendered":"https:\/\/python-programs.com\/?p=9218"},"modified":"2021-11-22T18:33:33","modified_gmt":"2021-11-22T13:03:33","slug":"python-program-to-print-numbers-in-a-range-1upper-without-using-any-loops-or-by-using-recursion","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-numbers-in-a-range-1upper-without-using-any-loops-or-by-using-recursion\/","title":{"rendered":"Python Program to Print Numbers in a Range (1,upper) Without Using any Loops or by Using Recursion"},"content":{"rendered":"
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list<\/a> is mandatory.<\/p>\n If we examine closely at this problem, we can see that the concept of “loop” is to track some counter value, such as “i=0″ till I = higher”. So, if we aren’t permitted to use loops, how can we track something in Python? 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 Below are the ways to print all the numbers in the range from 1 to upper without using any loops or by using recursive approach.<\/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 Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory. If we examine closely at this problem, we can see that the concept of “loop” is to track some counter value, such as “i=0″ till I = higher”. So, if we …<\/p>\n
\nOne option is to use ‘recursion,’ however we must be careful with the terminating condition. Here’s a solution that uses recursion to output numbers.<\/p>\nEnter some upper limit range = 11<\/pre>\n
The numbers from 1 to 11 without using loops : \r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11<\/pre>\n
Enter some upper limit range = 28<\/pre>\n
The numbers from 1 to 28 without using loops : \r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28<\/pre>\n
Program to Print Numbers in a Range (1,upper) Without Using any Loops\/Recursive Approach<\/h1>\n
\n
Method #1:Using Recursive function(Static Input)<\/h3>\n
\n
# function which prints numbers from 1 to gievn upper limit range\r\n# without loop\/by using recursion\r\n\r\n\r\ndef printNumbers(upper_limit):\r\n # checking if th upper limit value is greater than 0 using if statement\r\n if(upper_limit > 0):\r\n # If the number is more than zero, call the function again with\r\n # the input set to the number -1.\r\n printNumbers(upper_limit-1)\r\n # Print the upper limit\r\n print(upper_limit)\r\n\r\n\r\n# giveen upper limt range\r\nupper_limit = 28\r\nprint('The numbers from 1 to', upper_limit, 'without using loops : ')\r\n# passing the given upper limit range to printNumbers\r\nprintNumbers(upper_limit)\r\n<\/pre>\n
The numbers from 1 to 28 without using loops : \r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28<\/pre>\n
Method #2:Using Recursive function(User Input)<\/h3>\n
\n
# function which prints numbers from 1 to gievn upper limit range\r\n# without loop\/by using recursion\r\n\r\n\r\ndef printNumbers(upper_limit):\r\n # checking if th upper limit value is greater than 0 using if statement\r\n if(upper_limit > 0):\r\n # If the number is more than zero, call the function again with\r\n # the input set to the number -1.\r\n printNumbers(upper_limit-1)\r\n # Print the upper limit\r\n print(upper_limit)\r\n\r\n\r\n# giveen upper limt range\r\nupper_limit = int(input('Enter some upper limit range = '))\r\nprint('The numbers from 1 to', upper_limit, 'without using loops : ')\r\n# passing the given upper limit range to printNumbers\r\nprintNumbers(upper_limit)\r\n<\/pre>\n
Enter some upper limit range = 11\r\nThe numbers from 1 to 11 without using loops : \r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11<\/pre>\n
\n