Python Program to Print First 50 Natural Numbers Using Recursion

In the previous article, we have discussed Python Program to find the Normal and Trace of a Matrix

The task is to print the first 50 natural numbers using recursion.

Recursion:

Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.

Natural Numbers:

Natural numbers are a subset of the number system that includes all positive integers from one to infinity. Natural numbers, which do not include zero or negative numbers, are also known as counting numbers. They are a subset of real numbers that include only positive integers and exclude zero, fractions, decimals, and negative numbers.

Program to Print First 50 Natural Numbers Using Recursion in Python

Below are the ways to print the first 50 natural numbers using recursion.

Method : Using Recursion

Approach:

  • Take a variable say gvn_numb and initialize its value to 1 and store it in a variable.
  • Pass the given number as an argument to the NaturlNumbr¬†function.
  • Create a recursive function to say NaturlNumbr which takes the given number as an argument and returns the first 50 natural numbers.
  • Check if the given number is less than or equal to 50 using the if conditional statement.
  • If the statement is true, print the given number separated by spaces.
  • Pass the given number +1 as an argument to the NaturlNumbr function.{Recursive Logic}
  • Print the first 50 natural numbers using recursion.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say NaturlNumbr which takes the given number as an
# argument and returns the first 50 natural numbers.


def NaturlNumbr(gvn_numb):
    # Check if the given number is less than or equal to 50 using the if conditional
    # statement.
    if(gvn_numb <= 50):
      # If the statement is true, print the given number separated by spaces.
        print(gvn_numb, end=" ")
      # Pass the given number +1 as an argument to the NaturlNumbr function.{Recursive Logic}
        NaturlNumbr(gvn_numb + 1)


# Take a variable say gvn_numb and initialize its value to 1 and store it in a variable.
# Print the first 50 natural numbers using recursion.
gvn_numb = 1
# Pass the given number as an argument to the NaturlNumbr function.
print("The first 50 natural numbers are as follows:")
NaturlNumbr(gvn_numb)

Output:

The first 50 natural numbers are as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

Enhance your coding skills with our list of Python Basic Programs provided and become a pro in the general-purpose programming language Python in no time.