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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:22:14+00:00 2026-05-29T13:22:14+00:00

I still have problem to understand the ancient problem the tower of Hanoi recursion

  • 0

I still have problem to understand the ancient problem the tower of Hanoi recursion how it really works here. i have read it theoretically but i still don’t get how recursion is being called here. Can anybody explain each step what is going on for ex if the value of ring is 2.

In general i know that recursion it calls itself but here i get stuck:

public static void main(String[] args) {
// TODO Auto-generated method stub
    Scanner s = new Scanner(System.in);
    System.out.println("Input the number of rings");
    int rings = s.nextInt();
    move(rings, 'A', 'B', 'C');
}

public static void move(int rings, char x, char y, char z){
    if(rings > 0){
        move(rings - 1, x, z, y);
        System.out.println("Move ring " + rings + " from peg " + x + " to " + y + ".");
        move(rings - 1, z, y, x);   
    }
}

why when i give to ring value 1 it goes directly to this line:

System.out.println("Move ring " + rings + " from peg " + x + " to " + y + "."); 

Thank you.

  • 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-29T13:22:15+00:00Added an answer on May 29, 2026 at 1:22 pm

    When the value of rings is 1, what happens is the following:

    -move method is called with rings = 1
    -the condition is met (because 1 > 0 is true) so move is called with rings = 0
    -another instance of the move method starts, but this time rings = rings – 1 = 0
    -the condition is not met (because 0 > 0 is false) so nothing happens and that method ends
    -we’re back at the first instance of the method. Now, the System call is executed
    -after that, move is called again, with rings = rings – 1 = 0
    -another instance of the method starts, and again the condition is not met (0 > 0 is false), so the method is terminated without entering the “if” block

    The same will happen if rings equals 2, but a larger and more complex recursive tree will take place. The move method in which rings is 2 will call 2 move methods where rings equals 1, which will themselves call 2 move methods each in which rings is 0.

         2
        / \
      1     1
     / \   / \
    0   0 0   0
    

    Step by step, with rings = 2, this would happen:
    -move(1, x, z, y);
    -move(0, x, z, y);
    -System.out.println(“Move ring 1 from peg A to C.”);
    -move(0, z, y, x);
    -System.out.println(“Move ring 2 from peg A to B.”);
    -move(1, z, y, x);
    -move(0, x, z, y);
    -System.out.println(“Move ring 1 from peg C to B.”);
    -move(0, z, y, x);

    The value of rings in each call gives you a hint of where it’s happening. For example,
    move(1, x, z, y); happens in the method in which rings = 2, because the call is move(rings-1, x, z, y).

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that similar to : this question But I still don't
Hi I have a problem with my tabhost and I don't really understand why.
I still have a problem with the splash screen. I don't want to use
I have already made an algorithm for the problem but it still got a
I read a lot about handling rotation in android applications, but I still have
i still have a problem to develop a convenient app to the user. because
I have this problem again and again... and still have not a satisfactory answer...
I have a problem with an AJAX call in JQuery. It works on Chrome,
I have a problem in my code which i can't understand. I want to
I have a problem to understand how to use IoC in a scenario where

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.