Skip to content
Algorithms
  • Home
  • IDE
  • Site Map
  • Difficulty Level
    • Beginner
    • Intermediate
    • Expert
  • Interviews
    • Amazon Questions
    • Goldman Sachs
    • Google Interview
    • Microsoft Interview
    • Apache
    • NetApp
    • Adobe
    • Facebook
    • NVIDIA
    • Oracle

Print Numbers from 1 to N without using loop

August 31, 2019July 17, 2018 by Sumit Jain

Objective: Given a number N, write an program to print from number 1 to N without using loop.

Example:

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

Approach: Use Recursion

  • Make recursive to N = N-1.
  • In tail recursion, print the number

Code:

Output:

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

Related Posts:

  • Find Factorial of a given Number
  • Find all possible combinations with sum K from a given number N(1 to N) with the…
  • Print First N Prime Numbers - Java Code
  • Find all subsets of size K from a given number N (1 to N)
  • Reverse a given number – Java Code
  • Print First n numbers in Fibonacci Series
  • Print all subarrays using recursion
  • Fizz Buzz Challenge - Java Implementation
  • Find all unique combinations of numbers (from 1 to 9 ) with sum to N
  • Floyd’s Triangle – Java Implementation
  • Get the Sum of Digits in a number till it become a single digit
  • Find all the Armstrong numbers in the given range
  • Find all unique combinations of exact K numbers (from 1 to 9 ) with sum to N
  • Given an array, find all unique subsets with a given sum with allowed repeated digits.
  • Given an array, print all unique subsets with a given sum.
Categories Beginner, MISC, Recursion Tags Beginner Leave a comment
Post navigation
Swap two numbers using Bitwise XOR Operator
Floyd’s Triangle – Java Implementation



More Problems

  • Get the Sum of Digits in a number till it become a single digit
  • Find all the Armstrong numbers in the given range
  • Find all unique combinations of exact K numbers (from 1 to 9 ) with sum to N
  • Given an array, find all unique subsets with a given sum with allowed repeated digits.
  • Given an array, print all unique subsets with a given sum.
  • Given an array, Print sum of all subsets
  • Print Stack in reverse order.
  • Check if given number is Prime - O(√N) Solution – Java Program
  • Find all Prime Numbers less than equal to N | Set 1
  • Add digits until the number becomes a single digit




Subscribe ( No Spam!!)

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,418 other subscribers

Recent Posts

  • Find departure and destination cities from the itinerary
  • Rank Array Elements
  • Three Consecutive Odd Numbers
  • Non-decreasing Array with one allowed change
  • Duplicate zero’s without expanding the array.
  • Maximum Depth of Valid Nested Parentheses
  • Decimal to Binary
  • Maximum Consecutive Ones
  • Duplicate even elements in an array
  • Minimum Increments to make all array elements unique






© 2021 Algorithms • Powered by GeneratePress