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 3600092
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:27:04+00:00 2026-05-18T20:27:04+00:00

Given the following recursive function, what would be printed by mysterious(4)? void mysterious(int x)

  • 0

Given the following recursive function, what would be printed by mysterious(4)?

void mysterious(int x) {
    if (x == 0) return;
    printf(“%d ”, x);
    mysterious(x-1);
    mysterious(x-1);
}

Here is my call stack:

mysterious(4) => print 4
mysterious(3) => print 3
mysterious(2) => print 2
mysterious(1) => print 1
mysterious(0) => print 0

Is this correct?

  • 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-18T20:27:05+00:00Added an answer on May 18, 2026 at 8:27 pm

    Why don’t you just type it in to an editor in your language of choice, compile it and run? I chose Java but that’s just because I’m between CygWin installs on my box at the moment – I’d much rather be using C 🙂

    public class testprog {
        public static void mysterious(int x) {
            if (x == 0) return;
            System.out.print(x + " ");
            mysterious(x-1);
            mysterious(x-1);
        }
        public static void main(String args[]) {
            mysterious(4);
        }
    }
    

    This outputs the numbers:

    4 3 2 1 1 2 1 1 3 2 1 1 2 1 1
    

    Basically, what’s happening is that, at each level, you print out the number then call the next level twice with the next lowest number (unless it’s reached zero).

    Aside: technically, you do call the next layer with zero but, since it returns straight away, it has no affect on the output.

    The following diagram will hopefully illustrate this better, with different symbols representing different layers:

    (4) (-------------------------------) (-------------------------------)
         {3} {-----------} {-----------}   {3} {-----------} {-----------}
              [2] [1] [1]   [2] [1] [1]         [2] [1] [1]   [2] [1] [1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following recursive function, what would be printed by mysterious(4)? void mysterious(int x)
Given the following recursive function: // Pre-condition: y is non-negative. int mysterious(int x, int
I'm trying to write a recursive function that return a List < int *
Given the following recursive query: WITH DepartmentHierarchy (DepartmentID, Name, IsInactive, IsSpecial, ParentId, HierarchyLevel) AS
Given the following: List<List<Option>> optionLists; what would be a quick way to determine the
Given the following: declare @a table ( pkid int, value int ) declare @b
Given the following: &row->count Would &(row->count) be evaluated or (&row)->count be evaluated in C++?
Given the following code (that doesn't work): while True: # Snip: print out current
The following simple function applies a given monadic function iteratively until it hits a
Could anyone say why the following recursive function does not work for me ?

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.