Thursday, April 14, 2011

Tower of Hanoi iteration Count


import java.util.*;
public class HanoiTowerCount
{
                static double hanoi(int n)
        {
            if (n==0)
                return 1;
            else
                return(2*hanoi(n-1));
                }

                public static void main(String[] args)
    {
                                Scanner in=new Scanner(System.in);
                                System.out.println("Enter no. of rings please");
                                int n=in.nextInt();
                                double ans=hanoi(n);
                                System.out.println(--ans);
    }
}

No comments:

Post a Comment