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?
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.
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.
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 email@example.com. 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.
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space
- Print all n-digit strictly increasing numbers
- Print all possible expressions that evaluate to a target
- Print sums of all subsets of a given set
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Print matrix after applying increment operations in M ranges
- Count of numbers appearing in the given ranges at-least K times
- Position of n among the numbers made of 2, 3, 5 & 7
- Minimum number of prefix reversals to sort permutation of first N numbers
- Deriving the expression of Fibonacci Numbers in terms of golden ratio
- Print the nearest prime number formed by adding prime numbers to N
- Print all the combinations of a string in lexicographical order
- Number of divisors of product of N numbers
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
Improved By : jit_t