Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3334868
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:57:09+00:00 2026-05-17T23:57:09+00:00

If the number 5 is given to us, it needs to be printed out

  • 0

If the number 5 is given to us, it needs to be printed out on the console like 1, 2, 3, 4, 5. This needs to be done recursively. (Java preferably)

In case anyone is wondering these are not homework questions. I am practicing for my midterm a week from now.

Sorry about not posting my work. I was doing something like the below: but getting confused with where to print the rest of the number, and how to stop recursively calling the method with (n – 1). Jacob with his post helped me out. Thanks to everyone who helped.

public void writeNums(int n) {
    if (n < 1)
        throw new IllegalArgumentException();
    if (n == 1) {
        System.out.print("1, ");
    }
    writeNums(n - 1);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:57:10+00:00Added an answer on May 17, 2026 at 11:57 pm

    We’re not going to write your code for you, but the way recursion works is that you have a function that calls itself, passing in some parameter that changes for each call. It needs to handle the “base case” in which the function does something but doesn’t need to call itself anymore, and also handle the “general case” where the function both does something and calls itself to complete whatever needs to be done. So:

    function printNumbersUpTo( current )
        if current is 1, print 1 and return. //base case
        otherwise, call printNumbersUpTo( current - 1), then print current, and return. //general case
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function count_permutations() that returns the number of permutations of a given
How can I check if a given number is even or odd in C?
I have written a function that gets a given number of random records from
Given a number, how do I discover in what table and column it could
I'm looking for a method that computes the line number of a given text
I have a small math problem I am trying to solve Given a number
Given a credit card number and no additional information, what is the best way
Given a week number, e.g. date -u +%W , how do you calculate the
Given a sorted vector with a number of values, as in the following example:
If I have a certain week number (eg 51) and a given year (eg

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.