# Print numbers 1 to N using Indirect recursion

Given a number N, we need to print numbers from 1 to N with out direct recursion, loops, labels. Basically we need to insert in above code snippet so that it can be able to print numbers from 1 to N?

 `#include ` `#define N 20; ` `int` `main() ` `{  ` `   ``// Your code goes Here. ` `} `

Examples :

```Input  : 10
Output : 1 2 3 4 5 6 7 8 9 10

Input  : 5
Output : 1 2 3 4 5
```

We have already discussed solutions in below posts :
Print 1 to 100 in C++, without loop and recursion
How will you print numbers from 1 to 100 without using loop?

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

Here’s the code that can print the numbers from 1 to 100 with out direct recursion, loops and labels. The code uses indirect recursion.

## C

 `// C program to print from 1 to N using ` `// indirect recursion/ ` `#include ` ` `  `// We can avoid use of these using references ` `#define N 20; ` `int` `n = 1; ` ` `  `// Prints n, increments n and calls fun1() ` `void` `fun1() ` `{ ` `    ``if` `(n <= N) ` `    ``{ ` `        ``printf``(``"%d"``, n); ` `        ``n++; ` `        ``fun2(); ` `    ``} ` `    ``else` `        ``return``; ` `} ` ` `  `// Prints n, increments n and calls fun2() ` `void` `fun2() ` `{ ` `    ``if` `(n <= N) ` `    ``{ ` `        ``printf``(``"%d"``, n); ` `        ``n++; ` `        ``fun1(); ` `    ``} ` `    ``else` `        ``return``; ` `} ` ` `  `// Driver Program ` `int` `main(``void``) ` `{ ` `    ``fun1(); ` `    ``return` `0; ` `} `

## PHP

 ` `

Output :

```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
```

How This works?:
In above Above program, we just used two functions. One calls others and other one calls previous one, therefore indirect recursion.

Exercise :
Modify the above program to use N as a parameter instead of making it global.

This article is contributed by Umamaheswararao Tumma of Jntuh college of Engineering Hyderabad. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Improved By : jit_t

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.