Print numbers 1 to N using Indirect recursion
Last Updated :
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++ program to print from 1 to N using
// indirect recursion/
#include<bits/stdc++.h>
using namespace std;
// We can avoid use of these using references
int 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 Program
int main()
{
fun1();
return 0;
}
// This code is contributed by pankajsharmagfg.
C
// 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 Program
int main(void)
{
fun1();
return 0;
}
Java
// Java program to print from 1 to N using
// indirect recursion
class 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
Python3
# Python 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()
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 Program
if __name__ == '__main__':
fun1();
# This code is contributed by 29AjayKumar
C#
// C# program to print from 1 to N using
// indirect recursion
using 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
// 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 Code
fun1();
// This code is contributed
// by m_kit
?>
JavaScript
<script>
// Javascript program to print from 1 to N using
// indirect recursion
// We can avoid use of these using references
let 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 code
fun1();
// 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.
Similar Reads
Count Set-bits of number using Recursion Given a positive integer n, the task is to find the number of set bits in its binary representation using recursion.Examples: Input : 21 Output : 3 Explanation: 21 represented as 10101 in binary representation.Input : 16 Output : 1 Explanation: 16 represented as 10000 in binary representation.Using
3 min read
Product of 2 Numbers using Recursion Given two numbers x and y find the product using recursion.Examples : Input : x = 5, y = 2Output : 10Input : x = 100, y = 5Output : 500To find the product of two numbers x and y using recursion, you can use the following approach:Base Case: If y=0, return 0 (since any number multiplied by 0 is 0).Re
4 min read
Program to find all Factors of a Number using recursion Given a number N, the task is to print all the factors of N using recursion.Examples: Input: N = 16 Output: 1 2 4 8 16 Explanation: 1, 2, 4, 8, 16 are the factors of 16. A factor is a number which divides the number completely. Input: N = 8 Output: 1 2 4 8 Approach: The idea is to create a function
3 min read
Sum of natural numbers using recursion Given a number n, find sum of first n natural numbers. To calculate the sum, we will use a recursive function recur_sum().Examples : Input : 3 Output : 6 Explanation : 1 + 2 + 3 = 6 Input : 5 Output : 15 Explanation : 1 + 2 + 3 + 4 + 5 = 15 Below is code to find the sum of natural numbers up to n us
3 min read
Print 1 to 100 in C++ Without Loops and Recursion We can print 1 to 100 without using loops and recursion using three approaches discussed below: 1) Template Metaprogramming: Templates in C++ allow non-datatypes also as parameters. Non-datatype means a value, not a datatype. Example: CPP // CPP Program to print 1 to 100 // without loops and recursi
3 min read