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?
C
#include <stdio.h>
#define N 20;
int main()
{
}
|
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++
#include<bits/stdc++.h>
using namespace std;
int N = 20;
int n = 1;
void fun1();
void fun2();
void fun1()
{
if (n <= N)
{
cout << n << " " ;
n++;
fun2();
}
else
return ;
}
void fun2()
{
if (n <= N)
{
cout << n << " " ;
n++;
fun1();
}
else
return ;
}
int main()
{
fun1();
return 0;
}
|
C
#include<stdio.h>
#define N 20;
int n = 1;
void fun1()
{
if (n <= N)
{
printf ( "%d" , n);
n++;
fun2();
}
else
return ;
}
void fun2()
{
if (n <= N)
{
printf ( "%d" , n);
n++;
fun1();
}
else
return ;
}
int main( void )
{
fun1();
return 0;
}
|
Java
class GFG
{
static final int N = 20 ;
static int n = 1 ;
static void fun1()
{
if (n <= N)
{
System.out.printf( "%d " , n);
n++;
fun2();
}
else
{
return ;
}
}
static void fun2()
{
if (n <= N)
{
System.out.printf( "%d " , n);
n++;
fun1();
}
else
{
return ;
}
}
public static void main(String[] args)
{
fun1();
}
}
|
Python3
N = 20 ;
n = 1 ;
def fun1():
global N, n;
if (n < = N):
print (n, end = " " );
n + = 1 ;
fun2();
else :
return ;
def fun2():
global N, n;
if (n < = N):
print (n, end = " " );
n + = 1 ;
fun1();
else :
return ;
if __name__ = = '__main__' :
fun1();
|
C#
using System;
class GFG
{
static readonly int N = 20;
static int n = 1;
static void fun1()
{
if (n <= N)
{
Console.Write( "{0} " , n);
n++;
fun2();
}
else
{
return ;
}
}
static void fun2()
{
if (n <= N)
{
Console.Write( "{0} " , n);
n++;
fun1();
}
else
{
return ;
}
}
public static void Main(String[] args)
{
fun1();
}
}
|
PHP
<?php
$N = 20;
$n = 1;
function fun1()
{
global $N ;
global $n ;
if ( $n <= $N )
{
echo $n , " " ;
$n ++;
fun2();
}
else
return ;
}
function fun2()
{
global $N ;
global $n ;
if ( $n <= $N )
{
echo $n , " " ;
$n ++;
fun1();
}
else
return ;
}
fun1();
?>
|
Javascript
<script>
let N = 20;
let n = 1
function fun1()
{
if (n <= N)
{
document.write( n+ " " );
n++;
fun2();
}
else
{
return ;
}
}
function fun2()
{
if (n <= N)
{
document.write(n+ " " );
n++;
fun1();
}
else
{
return ;
}
}
fun1();
</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.
Last Updated :
13 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...