Print numbers 1 to N using Indirect recursion - GeeksforGeeks

13 May, 2022
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? 
C
#include <stdio.h>#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? 
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
Java
Python3
C#
PHP
Javascript
// C++ program to print from 1 to N using// indirect recursion/#include<bits/stdc++.h>using namespace std; // We can avoid use of these using referencesint N = 20;int n = 1; void fun1();void fun2(); // Prints n, increments n and calls fun1()void fun1(){    if (n <= N)    {        cout << n << " ";        n++;        fun2();    }    else        return;} // Prints n, increments n and calls fun2()void fun2(){    if (n <= N)    {        cout << n << " ";        n++;        fun1();    }    else        return;} // Driver Programint main(){    fun1();    return 0;} // This code is contributed by pankajsharmagfg.
// C program to print from 1 to N using// indirect recursion/#include<stdio.h> // 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 Programint main(void){    fun1();    return 0;}
// Java program to print from 1 to N using// indirect recursionclass GFG{    // We can avoid use of these using references    static final int N = 20;    static int n = 1;     // Prints n, increments n and calls fun1()    static void fun1()    {        if (n <= N)        {            System.out.printf("%d ", n);            n++;            fun2();        }        else        {            return;        }    }     // Prints n, increments n and calls fun2()    static void fun2()    {        if (n <= N)        {            System.out.printf("%d ", n);            n++;            fun1();        }        else        {            return;        }    }     // Driver Program    public static void main(String[] args)    {        fun1();    }} // This code is contributed by Rajput-Ji
# Python program to print from 1 to N using# indirect recursion # We can avoid use of these using referencesN = 20;n = 1; # Prints n, increments n and calls fun1()def fun1():    global N, n;    if (n <= N):        print(n, end = " ");        n += 1;        fun2();    else:        return; # Prints n, increments n and calls fun2()def fun2():    global N, n;    if (n <= N):        print(n, end = " ");        n += 1;        fun1();    else:        return; # Driver Programif __name__ == '__main__':     fun1(); # This code is contributed by 29AjayKumar
// C# program to print from 1 to N using// indirect recursionusing System; class GFG{    // We can avoid use of these using references    static readonly int N = 20;    static int n = 1;     // Prints n, increments n and calls fun1()    static void fun1()    {        if (n <= N)        {            Console.Write("{0} ", n);            n++;            fun2();        }        else        {            return;        }    }     // Prints n, increments n and calls fun2()    static void fun2()    {        if (n <= N)        {            Console.Write("{0} ", n);            n++;            fun1();        }        else        {            return;        }    }     // Driver Code    public static void Main(String[] args)    {        fun1();    }} // This code is contributed by Rajput-Ji
<?php// PHP program to print// from 1 to N using// indirect recursion // We can avoid use of// these using references$N = 20;$n = 1; // Prints n, increments// n and calls fun1()function fun1(){    global $N;    global $n;     if ($n <= $N)    {        echo $n, " ";        $n++;        fun2();    }    else        return;} // Prints n, increments// n and calls fun2()function fun2(){    global $N;    global $n;    if ($n <= $N)    {        echo $n, " ";        $n++;        fun1();    }    else        return;} // Driver Codefun1(); // This code is contributed// by m_kit?>
<script> // Javascript program to print from 1 to N using// indirect recursion // We can avoid use of these using referenceslet N = 20;let n = 1 // Prints n, increments n and calls fun1()function fun1(){    if (n <= N)    {        document.write( n+" ");        n++;        fun2();    }    else    {        return;    }} // Prints n, increments n and calls fun2()function fun2(){    if (n <= N)    {        document.write(n+" ");        n++;        fun1();    }    else    {        return;    }} // Driver codefun1(); // This code is contributed by rag2127 </script>
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  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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
Rajput-Ji
29AjayKumar
rag2127
pankajsharmagfg
simmytarika5
sagartomar9927
Algorithms
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
How to Start Learning DSA?
Difference between Algorithm, Pseudocode and Program
K means Clustering - Introduction
Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete
SCAN (Elevator) Disk Scheduling Algorithms
Quadratic Probing in Hashing
What are Hash Functions and How to choose a good Hash Function?
Difference between Informed and Uninformed Search in AI