# Program to print first n natural numbers using recursion in C

## First N natural numbers using Recursion in C Program Description:

Write a program to print the first n natural numbers using recursion in C programming language. This program should take an integer number from the user ( n) and print the first n natural numbers up to n i.e given number.

📢 Related Program: We have already looked at the program to print n natural numbers using Loops.

### Excepted Input and Output:

Input:

Enter a Number: 6

Output:

Numbers are : 1 2 3 4 5 6

## First N Natural Numbers using Recursion in C Algorithm:

1. Start the program by taking the user input and storing the input number in the variable n
2. Check for a negative number and display the error message if the n is a negative number. and allow the user to provide the input again using the Goto Statement
• We use the goto INPUT; statement to jump to the Label INPUT:. which prompts the user for input again.
3. Create a function named nNumbers, Which takes an integer number as input and uses recursion to print all numbers from 1 to n.
• As the nNumbers function is using the recursion to print the natural numbers, We need to have the base condition. The nNumbers function base condition is &nbsp;if(n == 0). The recursion will stop once the value of the n becomes zero.
• If the n is not zero, Then we will call the nNumbers function with n-1. which is <strong>nNumbers</strong>(n-1);
• The above recursive calls (function calls) continue until we reach the base condition. And before returning, every function will print the n.
• Once the above recursive function finishes The n natural numbers will be printed on the console.
4. Call the nNumbers function from the main function.

## First N Natural Numbers using Recursion in C Program:

Here are the prototype details of the nNumbers function.

• void nNumbers(int n);
• function_name: nNumbers
• arguments_list: int
• return_type: void

### Program Output:

Let’s compile and run the program. To compile use the following command

gcc &lt;filename.c&gt;

\$ gcc n-numbers-recursive.c

which generates an executable file named a.out. Run the executable using the ./a.out command like below.

Test Case 1: Positive Numbers:

We are getting the excepted results When user provided the input number as 15, The program generated the first 15 natural numbers.

Test Case 2: Negative Numbers:

As we can see from the above output, Whenever a user enters a negative number, Program displays an error message Error, Please enter positive number and asks for the user’s input again. This process continues until valid input from the user.

## Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

### 4 Responses

1. […] Print First N Natural Numbers using Recursion […]

2. […] C Program to Print First N Natural Numbers using Recursion […]

3. […] C Program to print first N Natural Numbers using Recursion […]

4. […] C Program to print first N Natural Numbers using Recursion […]